#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX = 25;
int n;
int map[MAX][MAX];
int answer = -1;
void MoveBlock(int m[MAX][MAX], int count) {
if (count == 10) {
return;
}
int copy[MAX][MAX];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
copy[i][j] = m[i][j];
}
}
for (int k = 0; k < 4; k++) {
int max_v = -1;
if (k == 0) { //아래
for (int j = 0; j < n; j++) {
int index = n;
int value = -1;
for (int i = n - 1; i >= 0; i--) {
if (m[i][j] == 0) {
continue;
}
if (m[i][j] == value) {
m[index][j] *= 2;
max_v = max(max_v, m[index][j]);
m[i][j] = 0;
value = -1;
}
else {
int tmp = m[i][j];
m[i][j] = 0;
index--;
m[index][j] =tmp;
max_v = max(max_v, m[index][j]);
value = m[index][j];
}
}
}
}
else if (k == 1) { //위
for (int j = 0; j < n; j++) {
int index = -1;
int value = -1;
for (int i = 0; i < n; i++) {
if (m[i][j] == 0) {
continue;
}
if (m[i][j] == value) {
m[index][j] *= 2;
max_v = max(max_v, m[index][j]);
m[i][j] = 0;
value = -1;
}
else {
int tmp = m[i][j];
m[i][j] = 0;
index++;
m[index][j] =tmp;
max_v = max(max_v, m[index][j]);
value = m[index][j];
}
}
}
}
else if (k == 2) { //오른쪽
for (int i = 0; i < n; i++) {
int index = n;
int value = -1;
for (int j = n - 1; j >= 0; j--) {
if (m[i][j] == 0) {
continue;
}
if (m[i][j] == value) {
m[i][index] *= 2;
max_v = max(max_v, m[i][index]);
m[i][j] = 0;
value = -1;
}
else {
int tmp = m[i][j];
m[i][j] = 0;
index--;
m[i][index] = tmp;
max_v = max(max_v, m[i][index]);
value = m[i][index];
}
}
}
}
else if (k == 3) { //왼쪽
for (int i = 0; i < n; i++) {
int index = -1;
int value = -1;
for (int j = 0; j < n; j++) {
if (m[i][j] == 0) {
continue;
}
if (m[i][j] == value) {
m[i][index] *= 2;
max_v = max(max_v, m[i][index]);
m[i][j] = 0;
value = -1;
}
else {
int tmp = m[i][j];
m[i][j] = 0;
index++;
m[i][index] = tmp;
max_v = max(max_v, m[i][index]);
value = m[i][index];
}
}
}
}
answer = max(answer, max_v);
bool flag = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (m[i][j] != copy[i][j]) {
flag = true;
break;
}
}
if (flag == true) break;
}
if (flag == true) {
if(answer < max_v * pow(2,9-count))
MoveBlock(m, count + 1);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
m[i][j] = copy[i][j];
}
}
}
}
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> map[i][j];
}
}
MoveBlock(map, 0);
cout << answer << endl;
}