class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {</int></int>

    vector<int> T;
    if(matrix.empty())
    {
        return T;
    }
    int j = 0;
    int i = 0 ;
    int top = 0;
    int bottom = matrix.size()-1;
    int left = 0;
    int right = matrix[0].size()-1;

    while(T.size() != matrix.size()* matrix[0].size())
    {
        while(j >=left  &&  j<= right)
        {
            T.push_back(matrix[i][j]);
            if(j ==  right)
            {
                top++;
                i++;
                break;
            }else{
                j++; 
            }

        }

        while( j ==  right && i >= top &&  i<= bottom)
        {
            //T[t] = matrix[i][j];
             T.push_back(matrix[i][j]);
            if(i ==  bottom)
            {
                right--;
                j--;
                break;
            }else{
                i++;
            }
        }

        while(i ==  bottom && j >=left &&  j<= right)
        {

           // T[t] = matrix[i][j];
             T.push_back(matrix[i][j]);
            if(j == left)
            {
                bottom --;
                i--;
                break;
            }else{
                 j--;
            }
       }

       while(j == left && i >= top && i<= bottom )
       {
           //T[t] = matrix[i][j];
           T.push_back(matrix[i][j]);
           if(i == top)
           {
               left ++;
                j++;
               break;
           }else{
               i--;
           }

       }
    }

   return  T;
}

};