Giving right output, But Wrong Answer.Why? (Problem Code: MINIMAX )

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n= s.nextInt();
int mat[][] = new int [n][n];
int maxOfMinRow=0, minOfMaxCol=1000001, count=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
mat[i][j] = s.nextInt();
}
}
int minInRow[] = new int[n];
int maxInCol[] = new int[n];
while(true)
{
for(int i=0; i<n; i++)
{
minInRow[i] = 1000001;
maxInCol[i] = 0;
}

             for(int i=0; i<n;i++)
            {
                for(int j=0; j<n-1; j++)
                {
                
                    if(mat[i][j] > mat[i][j+1])
                    {
                        if(mat[i][j+1] < minInRow[i])
                        {   
                            minInRow[i] = mat[i][j+1];
                        }
                   
                    }
                    else
                        {    
                        if  (mat[i][j] < minInRow[i])
                            {
                                minInRow[i] = mat[i][j];
                            } 
                        }
                        if(mat[j][i]< mat[j+1][i])
                        {
                            if(mat[j+1][i] > maxInCol[i])
                                {
                                    maxInCol[i] = mat[j+1][i];
                                }
                    
                        }
                        else
                        {
                            if(mat[j][i] > maxInCol[i])
                            {
                                maxInCol[i] = mat[j][i];
                            }
                        }
                   }
            }
        
        
            for(int k=0; k<n;k++)
            {
                
                if(minInRow[k] >= maxOfMinRow)
                {
                    maxOfMinRow = minInRow[k];
                    
                }
                if(maxInCol[k] <= minOfMaxCol)
                {
                    minOfMaxCol = maxInCol[k];
                    
                }
            
            }
            
               
            if(minOfMaxCol == maxOfMinRow)
            {
                System.out.println(count);
                break;
            }
            else
            {
                count++;
                outerloop:
                for(int i=0; i<n; i++)
                {
                    for(int j=0; j<n; j++)
                    {
                        if(mat[i][j] == minOfMaxCol)
                        {
                            mat[i][j] = maxOfMinRow;                               
                                
                            break outerloop;
                        }
                    }
                }
                
            }
        }
    }
    
}
//