#include <stdio.h>
#include <string.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define N 100
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int map[N][N],vis[N][N];
int n,L,H;
int small,big;
bool success;
void dfs(int x,int y)
{
int i,nx,ny;
if(map[x][y]<L || map[x][y]>H) return;
if(x==n-1 && y==n-1)
{
success=true;
return;
}
for(i=0;i<4;i++)
{
nx=x+dx[i];
ny=y+dy[i];
if(nx<0 || nx>=n || ny<0 || ny>=n || vis[nx][ny]) continue;
vis[nx][ny]=1;
dfs(nx,ny);
}
}
bool judge(int d)
{
int i;
for(i=small;i+d<=big;i++)
{
L=i;
H=i+d;
success=false;
memset(vis,0,sizeof(vis));
vis[0][0]=1;
dfs(0,0);
if(success) break;
}
if(i+d<=big) return true;
return false;
}
int main()
{
int i,j;
int min,max,mid;
while(~scanf("%d",&n))
{
small=120;
big=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&map[i][j]);
small=MIN(small,map[i][j]);
big=MAX(big,map[i][j]);
}
}
min=-1;
max=big-small;
while(min+1!=max)
{
mid=(min+max)>>1;
if(judge(mid)) max=mid;
else min=mid;
}
printf("%d\n",max);
}
return 0;
}