Wrong answer for n==21, but right for n ==5


#1
vector<vector<int> > Solution::generateMatrix(int n) {
     vector<vector<int>> nums(n, vector<int>(n, 0));

     if(n==1)return {{1}};
     int mx = n*n, curr=1 ;

for(int i=0; i<=(n+1)/2; i++){
    for(int j=i; j<n-i-1; j++){
        nums[i][j]=curr; curr++; if(curr>mx)return nums;
    }
    for(int r=i;r<n-i-1; r++){
        nums[r][n-1-i]=curr; curr++; if(curr>mx)return nums;
    }
    for(int c=n-1-i; c>i; c--){
        nums[n-1-i][c]=curr; curr++; if(curr>mx)return nums;
    }
    for(int r=n-1-i; r>i; r--){
        nums[r][i]=curr; curr++; if(curr>mx)return nums;
    }
}
return nums;

}

Getting WA for 21 but correct for n==5.
Why ?