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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TEwMAk0NQkxMDgJNjcJNgkxMwk5Mgk0NQkKNzAJMTAwCTM3CTU1CTQ4CTEwMgk3NQkxMDcJODAJNwk3OAkzNAkyMQkKODYJNzQJODEJMjQJMzMJOTkJMzYJNTkJMTA1CTM4CTY1CTUyCTE5CQoyMgkzNAk5Nwk2MwkzNgk4NAkyOAk1MQkzNAk0CTE4CTc4CTc4CQ==
13
20 4 27 47 19 69 80 75 64 34 101 55 15
2 65 85 35 71 94 93 82 56 109 18 25 36
19 11 103 2 54 92 41 58 89 74 63 91 74
109 107 10 5 93 104 103 28 108 50 84 78 99
91 33 44 37 59 32 48 109 69 84 79 5 30
59 71 37 60 78 94 107 21 102 4 87 92 17
95 30 104 44 106 104 105 78 66 74 60 59 76
66 3 46 46 11 63 57 9 13 46 66 70 44
47 9 41 66 87 104 98 22 15 95 89 93 30
19 10 37 75 98 100 45 108 67 6 13 92 45
70 100 37 55 48 102 75 107 80 7 78 34 21
86 74 81 24 33 99 36 59 105 38 65 52 19
22 34 97 63 36 84 28 51 34 4 18 78 78