uses math;
const maxn = 100;
oo = 200;
var n,low,high,maxa: byte;
a: array[0..maxn+1,0..maxn+1] of byte;
visit: array[0..maxn+1,0..maxn+1] of boolean;
cuoi,giua: byte;
procedure enter;
var i,j,mina:byte;
begin
readln(n);
maxa:=0;
mina:=oo;
for i:=1 to n do
begin
for j:=1 to n do
begin
read(a[i,j]);
if a[i,j]<mina then mina:=a[i,j];
if a[i,j]>maxa then maxa:=a[i,j];
end;
readln;
end;
cuoi:=maxa-mina;
end;
{$M 400000000}
function dfs(x,y:byte):boolean;
var p:boolean;
begin
visit[x,y]:=true;
if (a[x,y]>high) or (a[x,y]<low) then dfs:=false
else
if (x=n) and (y=n) then dfs:=true
else
begin
p:=false;
if (x>1) and (not visit[x-1,y]) then p:=dfs(x-1,y);
if (not p) and (x<n) and (not visit[x+1,y]) then p:=dfs(x+1,y);
if (not p) and (y>1) and (not visit[x,y-1]) then p:=dfs(x,y-1);
if (not p) and (y<n) and (not visit[x,y+1]) then p:=dfs(x,y+1);
dfs:=p;
end;
end;
function check:boolean;
var p:boolean;
i,j:byte;
begin
p:=false;
for low:=0 to maxa-giua do
begin
high:=low+giua;
for i:=1 to n do
for j:=1 to n do
visit[i,j]:=false;
if dfs(1,1) then
begin
p:=true;
break;
end;
end;
check:=p;
end;
procedure tknp;
var i,j,dau: byte;
begin
dau:=0;
for giua:=dau to cuoi do
begin
if check then break;
end;
writeln(giua);
end;
begin
enter;
tknp;
end.
dXNlcyBtYXRoOwpjb25zdCBtYXhuID0gMTAwOwogICAgICBvbyA9IDIwMDsKdmFyIG4sbG93LGhpZ2gsbWF4YTogYnl0ZTsKICAgIGE6IGFycmF5WzAuLm1heG4rMSwwLi5tYXhuKzFdIG9mIGJ5dGU7CiAgICB2aXNpdDogYXJyYXlbMC4ubWF4bisxLDAuLm1heG4rMV0gb2YgYm9vbGVhbjsKICAgIGN1b2ksZ2l1YTogYnl0ZTsKcHJvY2VkdXJlIGVudGVyOwp2YXIgaSxqLG1pbmE6Ynl0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