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.
  