import java.io.*;

public class Main {
    public static String[][] board;
    public static int n;
    public static int max;
        
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        board = new String[n+1][n+1];
        max = 1;
        
        for(int i=1; i<=n; i++) {
            String[] arr = br.readLine().split("");
            for(int j=1; j<=n; j++) { board[i][j] = arr[j-1]; }
        }
        
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=n; j++) {
                if((i==n&&j==n) || max==n) { System.out.println(max); return; }
                
                countC(i, j);
                countR(i, j);
                
                if(i==n) {
                    swapC(i ,j);
                    countC(i, j);
                    countR(i, j);
                    countR(i, j+1);
                    swapC(i ,j);
                    continue;
                } else if(j==n) {
                    swapR(i ,j);
                    countR(i, j);
                    countC(i, j);
                    countC(i+1, j);
                    swapR(i ,j);
                    continue;
                }
                
                swapC(i ,j);
                countC(i, j);
                countR(i, j);
                countR(i, j+1);
                swapC(i ,j);
                
                swapR(i ,j);
                countR(i, j);
                countC(i, j);
                countC(i+1, j);
                swapR(i ,j);
            }
        }
        
        System.out.println(max);
    }
    
    public static void countC(int i, int j) {
        int count = 1;
        for(int k=2; k<=n; k++) {
            if(board[i][k].equals(board[i][k-1])) {
                count++;
                max = Math.max(max, count);
            } else { count = 1; }
        }        
    }
    
    public static void countR(int i, int j) {
        int count = 1;
        for(int k=2; k<=n; k++) {
            if(board[k][j].equals(board[k-1][j])) {
                count++;
                max = Math.max(max, count);
            } else { count = 1; }
        }        
    }
    
    public static void swapC(int i, int j) {
        String temp = board[i][j];
        board[i][j] = board[i][j+1];
        board[i][j+1] = temp;
    }
    
    public static void swapR(int i, int j) {
        String temp = board[i][j];
        board[i][j] = board[i+1][j];
        board[i+1][j] = temp;
    }
}

'Problem Solving > BOJ' 카테고리의 다른 글

[Gold 5] 1107번 리모컨  (0) 2022.04.01
[Silver 5] 1476번 날짜 계산  (0) 2022.04.01
[Gold 4] 17404번 RGB거리 2  (0) 2022.03.29
[Gold 5] 2133번 타일 채우기  (0) 2022.03.29
[Gold 5] 13398번 연속합 2  (0) 2022.03.29