import java.util.ArrayList;
/*
round=0;
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
*/
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
        int height=matrix.length;
        int width=matrix[0].length;

        if(width==0||height==0){
            return null;
        }

        ArrayList<Integer>result=new ArrayList<>();

        int h=height;
        int w=width;

        int round=0;

        int roundMax=0;
        if(height>=width){
            roundMax=width/2;
            if(width%2==1){
                roundMax++;
            }
        }
        if(width>=height){
            roundMax=height/2;
            if(height%2==1){
                roundMax++;
            }
        }


        while(round<=roundMax-1){
            boolean h1=false;
            boolean r1=false;
            if(w-round-round>1){
                r1=true;
            }
            for(int i=round;i<w-round;i++){
                result.add(matrix[round][i]);
            }
            for(int i=round+1;i<h-round;i++){
                result.add(matrix[i][w-1-round]);
                h1=true;
            }
            if(h1){
                for(int i=w-2-round;i>=round;i--){
                    result.add(matrix[h-1-round][i]);
                }
            }
            if(r1){
                for(int i=h-2-round;i>=round+1;i--){
                    result.add(matrix[i][round]);
                }
            }
            round++;
        }
        return result;
    }
}