import java.util.*;
public class Solution {
    public static List<List<Integer>> transposeMatrix(List<List<Integer>> matrix) {

        List<Integer> ll = new ArrayList<Integer>();

        List<List<Integer>> finalMatrix = new ArrayList<List<Integer>>();

        int size=0;
        int matrixSize=matrix.size();
        int posInList=0;
        int numItemsInList=0;
        int counter=0;
        int i;

//this gets number items in list
for (List<Integer> ee: matrix)
        {
            for (int k: ee)
            {
                numItemsInList++;
            }
            break;

        }


        do
        {
        
        //we need the posInList to be outer
        //we also know it has to go through all items in
        //List<Integer>
        //I am hoping having double nesting in the for loop
        //will provide flow control.
        //we know most outer will execute once, the next inner once and most inner maximum iterations
        for (i=posInList;i<numItemsInList;i++)

        //issue is it would have no iterated to the next List<Integer>
        for (List<Integer> ee: matrix)
        {
            //This now goes through all the numbers
            //in List<Integer>
            //we are only concerned with getting the first number
            //from each List<Integer>, next time round the 2nd index.....
            //so we have to draw comparisons to i in outer for loop

            for (int m=finalMatrix.size(); m<ee.size(); m++)
            {
                System.out.println("WHOLE LIST: " + ee);
                System.out.println("COUNTER: " + counter);
                System.out.println("i: " + i);

                if (counter==i)
                {
                    System.out.println("val i: " + i);

                   

                    ll.add(ee.get(m));
                    System.out.println("STORING: " + m);
                    System.out.println("matrix entry: " + ll);
                    

                    //increasing counter will ensure it only enters
                    //once in if statement
                    if (finalMatrix.size()<1)
                    {
                    counter++;
                    }
                    //System.out.println("NEW COUNTER INDEX IS: " + counter);
                    if (finalMatrix.size()<1)
                    { 
                    i++;
                    }
                    System.out.println("VALUE OF I: " + i);

                    if (counter==numItemsInList || ((finalMatrix.size()==1) && (counter+1==numItemsInList) ))
                    {
                        //posInList++;
                        //counter=0;
                        finalMatrix.add(ll);
                        System.out.println("************current matrix: " + finalMatrix);
                        ll.clear();
                        counter=0;
                        System.out.println("POSTION: " + posInList);
                    }

                    //since we are pairing up as part of transposing

                    /*
                    if (counter!=0 && counter%2==0)
                    {
                        matrix.add(ll);
                        ll.clear();
                    }
                    */

                    break;
                }

                if (finalMatrix.size()<1)
                {
                counter++;
                }
                else
                {
                    if (ll.isEmpty())
                    {
                        counter++;
                    }
                }
            }
        
        }

        
        //these two variables would go up together
        //no need having both size and posInList
        size++;
        posInList++;
        i=posInList;

        ll.clear();

        //this statement is incorrect
        //}while (size<matrixSize);

        //WE NEED TO END ONCE IT HAS PROCESSED
        //ALL ITEMS IN List<Integer>.
        //It would be uniform
        }while (posInList<numItemsInList);

        return finalMatrix;
    }
}