{$M 40000000}
const maxm = 1000;
      maxn = 1000;
      maxq = 10000000;
      oo = 100000000;
type o = record
           x,y:word;
         end;
     tqueue = record
                front,rear: longint;
                item: array[0..maxq-1] of o;
              end;
var n,m,sx,sy,fx,fy:word;
    a: array[0..maxm+1,0..maxn+1] of longint;
    queue: tqueue;
procedure initq;
begin
  with queue do
  begin
    front:=0;
    rear:=1;
  end;
end;
function qempty:boolean;
begin
  with queue do
  qempty:=(rear>front);
end;
procedure push(x,y:word);
begin
  with queue do
  begin
    front:=(front+1) mod maxq;
    item[front].x:=x;
    item[front].y:=y;
  end;
end;
function pop:o;
begin
  with queue do
  begin
    pop:=item[rear];
    rear:=(rear+1) mod maxq;
  end;
end;
procedure enter;
var i,j:word;
begin
  initq;
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      read(a[i,j]);
      if a[i,j]=0 then push(i,j)
      else a[i,j]:=oo;
    end;
    readln;
  end;
  readln(sx,sy,fx,fy);
end;
procedure printa;
var i,j:word;
begin
  for i:=1 to n do
  begin
    for j:=1 to m do
    write(a[i,j],' ');
    writeln;
  end;
  writeln;
end;
procedure qhd;
var p:o;
begin
  while not qempty do
  begin
    p:=pop;
    with p do
    begin
      if (x<n) and (a[x+1,y]>a[x,y]+1) then
      begin
        a[x+1,y]:=a[x,y]+1;
        push(x+1,y);
      end;
      if (x>1) and (a[x-1,y]>a[x,y]+1) then
      begin
        a[x-1,y]:=a[x,y]+1;
        push(x-1,y);
      end;
      if (y<m) and (a[x,y+1]>a[x,y]+1) then
      begin
        a[x,y+1]:=a[x,y]+1;
        push(x,y+1);
      end;
      if (y>1) and (a[x,y-1]>a[x,y]+1) then
      begin
        a[x,y-1]:=a[x,y]+1;
        push(x,y-1);
      end;
    end;
  end;
end;
procedure tknp;
var dau,cuoi,giua,i,j:longint;
    visit: array[1..maxn,1..maxm] of boolean;
  function dfs(x,y:longint):boolean;
  var f:boolean;
  begin
    if not visit[x,y] then visit[x,y]:=true
    else
    begin
      dfs:=false;
      exit;
    end;
    f:=false;
    if a[x,y]>giua then
    begin
      dfs:=false;
      exit;
    end
    else
    if (x=fx) and (y=fy) then f:=true
    else
    begin
      if (not f) and (x<n) then f:=dfs(x+1,y);
      if (not f) and (x>1) then f:=dfs(x-1,y);
      if (not f) and (y<m) then f:=dfs(x,y+1);
      if (not f) and (y>1) then f:=dfs(x,y-1);
    end;
    dfs:=f;
  end;
begin
  dau:=0;
  cuoi:=m*n;
  repeat
    for i:=1 to n do
    for j:=1 to m do
    visit[i,j]:=false;
    giua:=(dau+cuoi) div 2;
    if dfs(sx,sy) then
    cuoi:=giua-1
    else
    dau:=giua+1;
  until dau>cuoi;
  writeln(dau);
end;
begin
  enter;
  qhd;
  tknp;
end.

