#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=100010;
const int MAX=1000000100;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
struct node {
int l,e,s;
node() {}
node(int l,int e,int s):l(l),e(e),s(s) {}
}f[1010][1010][8];
char s[1010];
int g[8][2]={0,-1,-1,-1,-1,0,-1,1,0,1,1,1,1,0,1,-1};
int a[1010][1010];
node get(int x,node k) {
node ret=node(0,0,0);
if (x==0) return ret;
ret=k;ret.l++;
if (x==2) ret.e++;
else if (x==3) ret.s++;
return ret;
}
int pd(int a,int b,int c,int d) {
return (a-c)*log(2)+(b-d)*log(3)>=0;
}
int main()
{
int i,j,k,h,n,mx=0,x=0,y=0,xx,yy;
ll ans;
scanf("%d", &n);
for (i=1;i<=n;i++) {
scanf("%s", s);
for (j=1;j<=n;j++) {
a[i][j]=s[j-1]-'0';mx=max(mx,a[i][j]);
}
}
memset(f,0,sizeof(f));
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if (a[i][j]) {
for (k=0;k<4;k++) f[i][j][k]=get(a[i+g[k][0]][j+g[k][1]],f[i+g[k][0]][j+g[k][1]][k]);
}
for (i=n;i>=1;i--)
for (j=n;j>=1;j--)
if (a[i][j]) {
for (k=4;k<8;k++) f[i][j][k]=get(a[i+g[k][0]][j+g[k][1]],f[i+g[k][0]][j+g[k][1]][k]);
}
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if (a[i][j]) {
xx=yy=0;
if (a[i][j]==2) xx++;
else if (a[i][j]==3) yy++;
k=min(f[i][j][0].l,min(f[i][j][2].l,min(f[i][j][4].l,f[i][j][6].l)));
for (h=0;h<=6;h+=2) {
xx+=f[i][j][h].e-f[i+k*g[h][0]][j+k*g[h][1]][h].e;
yy+=f[i][j][h].s-f[i+k*g[h][0]][j+k*g[h][1]][h].s;
}
if (pd(xx,yy,x,y)) { x=xx;y=yy; }
xx=yy=0;
if (a[i][j]==2) xx++;
else if (a[i][j]==3) yy++;
k=min(f[i][j][1].l,min(f[i][j][3].l,min(f[i][j][5].l,f[i][j][7].l)));
for (h=1;h<=7;h+=2) {
xx+=f[i][j][h].e-f[i+k*g[h][0]][j+k*g[h][1]][h].e;
yy+=f[i][j][h].s-f[i+k*g[h][0]][j+k*g[h][1]][h].s;
}
if (pd(xx,yy,x,y)) { x=xx;y=yy; }
}
if (x==0&&y==0) printf("%d\n", mx);
else {
ans=1;
for (i=1;i<=x;i++) ans=ans*2%MOD;
for (i=1;i<=y;i++) ans=ans*3%MOD;
printf("%I64d\n", (ans+MOD)%MOD);
}
return 0;
}