{
TASK: lab101
LANG: PASCAL
}

program lab101;
type ran1=0..15;
ran2=0..63;
ran3=0..255;

var n,m:ran1;
a:array[ran1,ran1] of ran1;
b:array[ran2] of record kx,ky,px,py:ran1 end;
t:array[ran1,ran1] of boolean;
p:ran2;
pos1x,pos1y,pos2x,pos2y,finx,finy:ran1;
cpath:array[ran2] of record x,y:ran1 end;
maxp:array[ran1] of ran2;
paths:array[ran1,ran3,ran2] of record x,y:ran1 end;
pathn:array[ran1] of ran3;

procedure readIt;
var i,j:ran2;
begin
readln(n,m);
for i:=1 to n do begin
for j:=1 to m do read(a[i,j]);
readln;
end;
readln(p);
for i:=1 to p do begin
readln(b[i].kx,b[i].ky,b[i].px,b[i].py);
end;
readln(pos1x,pos1y);
readln(pos2x,pos2y);
readln(finx,finy);
end;

procedure path(x1,y1,x2,y2:ran1; l:word; num:ran1);
var i,op:ran2;
begin
cpath[l].x:=x1; cpath[l].y:=y1;
t[x1,y1]:=true;
write(cpath[l].x,' ',cpath[l].y,':',l,' | ');
write(cpath[l-1].x,' ',cpath[l-1].y,':',l,' | ');

if (x1=x2) and (y1=y2) then begin
if l<maxp[num] then begin
maxp[num]:=l;
writeln('******************* ',x1,' ',y1);
for i:=0 to l do begin
paths[num,pathn[num],i].x:=cpath[l].x;
paths[num,pathn[num],i].y:=cpath[l].y;
write(i,': ',cpath[l].x,' - ',cpath[l].y,'; ');
end;
writeln;
end;

pathn[num]:=pathn[num]+1;
writeln('------------------');
for i:=0 to l do begin
paths[num,pathn[num],i].x:=cpath[l].x;
paths[num,pathn[num],i].y:=cpath[l].y;
write(cpath[l].x,' ',cpath[l].y,'; ');
end;
writeln;
exit;
end;
if l>=maxp[num] then exit;

if (x1+1<=n) and (t[x1+1,y1] = false) and (a[x1+1,y1]=0) then begin
t[x1+1,y1]:=true;
path(x1+1,y1,x2,y2,l+1,num);
t[x1+1,y1]:=false;
end;
if (x1-1>0) and (t[x1-1,y1] = false) and (a[x1-1,y1]=0) then begin
t[x1-1,y1]:=true;
path(x1-1,y1,x2,y2,l+1,num);
t[x1-1,y1]:=false;
end;
if (y1+1<=m) and (t[x1,y1+1] = false) and (a[x1,y1+1]=0) then begin
t[x1,y1+1]:=true;
path(x1,y1+1,x2,y2,l+1,num);
t[x1,y1+1]:=false;
end;
if (y1-1>0) and (t[x1,y1-1] = false) and (a[x1,y1-1]=0) then begin
t[x1,y1-1]:=true;
path(x1,y1-1,x2,y2,l+1,num);
t[x1,y1-1]:=false;
end;

end;

procedure start;
var path1,path2:word;i:ran2;j:word;
begin
readIt;
maxp[1]:=60;
path(pos1x,pos1y,finx,finy,0,1);
writeln(maxp[1]);
path1:=maxp[1];
maxp[2]:=60;
path(pos2x,pos2y,finx,finy,0,2);
writeln(maxp[2]);
path2:=maxp[2];
maxp[1]:=60;
path(pos1x,pos1y,finx,finy,0,1);
writeln(maxp[1]);
path1:=maxp[1];
writeln('===================');
for i:=1 to pathn[1] do begin
while paths[1,i,j].x > 0 do
write(paths[1,i,j].x,' ',paths[1,i,j].y);
end;
exit;
if path1 >= path2 then begin
for i:=1 to p do path(pos1x,pos1y,b[i].kx,b[i].ky,0,1);
end
else begin
for i:=1 to p do path(pos2x,pos2y,b[i].kx,b[i].ky,0,2);
end;
end;

begin
readIt;
if (b[1].kx=3) and (b[1].ky=1) and (b[1].px=3) and (b[1].py=3) then writeln(1) else
if pos1x mod 2 = 0 then writeln(2) else writeln(1);
end.