class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        vector<int> list;
        int up = 0;
        int down = matrix.size()-1;
        int left = 0;
        int right = matrix[0].size()-1;
        while(true) {
            for(int i=left;i<=right;i++)
                list.push_back(matrix[up][i]);
            up++;
            if(up>down)
                break;
            for(int i=up;i<=down;i++)
                list.push_back(matrix[i][right]);
            right--;
            if(left>right)
                break;
            for(int i=right;i>=left;i--)
                list.push_back(matrix[down][i]);
            down--;
            if(up>down)
                break;
            for(int i=down;i>=up;i--)
                list.push_back(matrix[i][left]);
            left++;
            if(left>right)
                break;
        }
        return list;
    }
};