{$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.
eyRNIDQwMDAwMDAwfQpjb25zdCBtYXhtID0gMTAwMDsKICAgICAgbWF4biA9IDEwMDA7CiAgICAgIG1heHEgPSAxMDAwMDAwMDsKICAgICAgb28gPSAxMDAwMDAwMDA7CnR5cGUgbyA9IHJlY29yZAogICAgICAgICAgIHgseTp3b3JkOwogICAgICAgICBlbmQ7CiAgICAgdHF1ZXVlID0gcmVjb3JkCiAgICAgICAgICAgICAgICBmcm9udCxyZWFyOiBsb25naW50OwogICAgICAgICAgICAgICAgaXRlbTogYXJyYXlbMC4ubWF4cS0xXSBvZiBvOwogICAgICAgICAgICAgIGVuZDsKdmFyIG4sbSxzeCxzeSxmeCxmeTp3b3JkOwogICAgYTogYXJyYXlbMC4ubWF4bSsxLDAuLm1heG4rMV0gb2YgbG9uZ2ludDsKICAgIHF1ZXVlOiB0cXVldWU7CnByb2NlZHVyZSBpbml0cTsKYmVnaW4KICB3aXRoIHF1ZXVlIGRvCiAgYmVnaW4KICAgIGZyb250Oj0wOwogICAgcmVhcjo9MTsKICBlbmQ7CmVuZDsKZnVuY3Rpb24gcWVtcHR5OmJvb2xlYW47CmJlZ2luCiAgd2l0aCBxdWV1ZSBkbwogIHFlbXB0eTo9KHJlYXI+ZnJvbnQpOwplbmQ7CnByb2NlZHVyZSBwdXNoKHgseTp3b3JkKTsKYmVnaW4KICB3aXRoIHF1ZXVlIGRvCiAgYmVnaW4KICAgIGZyb250Oj0oZnJvbnQrMSkgbW9kIG1heHE7CiAgICBpd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