#include<cstdio>
const int MAX = 99;
char imap[MAX][MAX];
int dr[] = { 0, 1 }; // 0:right , 1:down
int dc[] = { 1, 0 };
// right col col+1 row
// down row row+1 col
int n;
int check_row(int row) {
int ret = 0;
char pivot = imap[row][0];
int ans = 1;
for (int i = 1; i < n; i++) {
char there = imap[row][i];
if (there == pivot) ans++;
else if (there != pivot) ans = 1, pivot = there;
ret = (ret > ans) ? ret : ans;
}
return ret;
}
int check_col(int col) {
int ret = 0;
char pivot = imap[0][col];
int ans = 1;
for (int i = 1; i < n; i++) {
char there = imap[i][col];
if (there == pivot) ans++;
else if (there != pivot) ans = 1, pivot = there;
ret = (ret > ans) ? ret : ans;
}
return ret;
}
void imap_swap(int hr, int hc, int tr, int tc) {
char sav = imap[hr][hc];
imap[hr][hc] = imap[tr][tc];
imap[tr][tc] = sav;
}
int max2(int a, int b) {
return (a > b) ? a : b;
}
bool is_diff(int hr, int hc, int tr, int tc) {
return (imap[hr][hc] != imap[tr][tc]) ? true : false;
}
int change_down_check(int row, int col) {
int ret = 0;
ret = max2(ret, check_col(col));
ret = max2(ret, check_row(row));
if (row + 1 < n) ret = max2(ret, check_row(row + 1));
return ret;
}
int change_right_check(int row, int col) {
int ret = 0;
ret = max2(ret, check_row(row));
ret = max2(ret, check_col(col));
if (col + 1 < n) ret = max2(ret, check_col(col + 1));
return ret;
}
int change(int row, int col) {
// right col col+1 row
// down row row+1 col
int ret = 0;
if (n - 1 == row && n - 1 == col) return 0; // add_code
if (n - 1 == row) { // 마지막 행
int i = 0;
int tr = row + dr[i];
int tc = col + dc[i];
if(is_diff(row,col, tr,tc)){
imap_swap(row, col, tr, tc);
ret = max2(ret, change_right_check(row, col));
imap_swap(row, col, tr, tc);
}
}
else if (n - 1 == col) { // 마지막 열
int i = 1;
int tr = row + dr[i];
int tc = col + dc[i];
if (is_diff(row, col, tr, tc)) {
imap_swap(row, col, tr, tc);
ret = max2(ret, change_down_check(row, col));
imap_swap(row, col, tr, tc);
}
}
else {
for (int i = 0; i < 2; i++) {
int tr = row + dr[i];
int tc = col + dc[i];
if (0 == i) { // right
if (is_diff(row, col, tr, tc)) {
imap_swap(row, col, tr, tc);
ret = max2(ret, change_right_check(row, col));
imap_swap(row, col, tr, tc);
}
}
else if (1 == i) { // down
if (is_diff(row, col, tr, tc)) {
imap_swap(row, col, tr, tc);
ret = max2(ret, change_down_check(row, col));
imap_swap(row, col, tr, tc);
}
}
}
}
return ret;
}
int answer;
int main() {
scanf("%d", &n);
for (int r = 0; r < n; r++) scanf("%s", &imap[r]);
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
if(r == 0) answer = max2(answer, check_col(c)); // check_row -> check_col
if(c == 0) answer = max2(answer, check_row(r)); // 2n
answer = max2(answer, change(r, c));
}
}
printf("%d", answer);
return 0;
}
// 1: 100% WA
// 2: 100% WA
// 3: 92% WA
// 4: 92% WA