class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& ma) {
int row=ma.size(), col=ma[0].size();
int top=0,bot=row-1 ,left=0, right=col-1;
vector<int> res;
while(top<=bot && left<=right){
for(int i=left;i<=right;i++){
res.push_back(ma[top][i]);
}
top++;
for(int i=top;i<=bot;i++){
res.push_back(ma[i][right]);
}
right--;
if(top<=bot){ //for single rows
for(int i=right;i>=left;i--){
res.push_back(ma[bot][i]);
}
bot--;
}
if(left<=right){
for(int i=bot;i>=top;i--){
res.push_back(ma[i][left]);
}
left++;
}
}
return res;
}
};