class Solution {
public:
    vector<int>v;
    vector<vector<int> > mv;
    int n,m;
    void func(int k){
        //边界条件
        if(2*k+1>min(n,m))return;
        //依次打印
        for(int i=k;i<=m-k-1;i++)v.push_back(mv[k][i]);
        for(int i=k+1;i<=n-k-1;i++)v.push_back(mv[i][m-k-1]);
        
        if(2*k+2>min(n,m))return;
        for(int i=m-k-2;i>=k;i--)v.push_back(mv[n-k-1][i]);
        for(int i=n-k-2;i>=k+1;i--)v.push_back(mv[i][k]);
        func(k+1);
    }
    vector<int> printMatrix(vector<vector<int> > matrix) {
        v.clear();
        n=matrix.size();
        if(n==0)return v;
        m=matrix[0].size();
        if(m==0)return v;
        mv=matrix;
        func(0);
        return v;
    }
};