{
TASK: lab101
LANG: PASCAL
}

{$R-}
Const
maxN = 11;
maxM = 11;
Type
Pts = record
        x, y, x1, y1 : longint;
      end;
var
a : array[0..maxN, 0..maxM] of longint;
Used : array[1..2, 0..maxN, 0..maxN] of longint;
p : array[1..32] of Pts;
U : array[1..32] of boolean;
max : array[1..2]  of longint;
h : array[1..2] of Pts;
Ps : array[0..2] of Pts;
tt : array[1..2] of longint;
br, x1, y1, x2, y2, x, y, i, j, k, l, m, n, pp : longint;
f : text;

Function Check(k, x, y : longint):boolean;
begin
if (x>n) or (y>m) or (x<=0) or (y<=0) or (a[x, y]=1) or (Used[k, x, y]>1) then
  Check:=false
else
  Check:=true;
end;


Function Cool(var k : longint; x, y, play : longint) : boolean;
var
i : longint;
begin
for i:=1 to pp do
  if (p[i].x=x) and (p[i].y=y) then
    break;
k:=i;
if (not u[k]) and (p[i].x=x) and (p[i].y=y) and ( (Ps[play].x<>p[i].x1) or (Ps[play].y<>p[i].y1) ) then
  begin
    U[k]:=true;
    Cool:=true;
  end
else
  Cool:=false;
End;



Procedure rekur(player : byte; move : longint);
begin
if br mod 20 = 0 then
if (h[player].x=Ps[player].x) and (h[player].y=h[player].y) then
  inc(tt[player]);

inc(br);
if (Ps[player-1].x=3) and (Ps[player-1].y=3) then
  x:=x;
if br > 10000000 then
  begin
    if tt[1]>5 then
      max[1]:=999999;
    if tt[2]>5 then
      max[2]:=999999;

    if max[1]>max[2] then
      writeln(2)
    else
      if max[2]>max[1] then
        writeln(1)
      else
        writeln(0);
    halt;
  end
else
if (Ps[player div 2 +1].x=x) and (Ps[player div 2 + 1].y=y) then
  begin
    if max[player div 2 + 1]<move then
      max[player div 2 +1 ]:=move;
  end
else
  begin
    if Check(player, Ps[player].x+1, Ps[Player].y) then
      begin
        Ps[player].x:=Ps[player].x+1;
        Ps[player].y:=Ps[player].y;
        inc(Used[player, Ps[player].x, Ps[Player].y]);
        rekur(player mod 2 + 1, move);
        rekur(player, move+1);
        Ps[player].x:=Ps[player].x-1;
        Ps[player].y:=Ps[player].y;
      end;

    if Check(player, Ps[player].x-1, Ps[Player].y) then
      begin
        Ps[player].x:=Ps[player].x-1;
        Ps[player].y:=Ps[player].y;
        inc(Used[Player, Ps[player].x, Ps[Player].y]);
        rekur(player mod 2 + 1, move);
        rekur(player, move+1);
        Ps[player].x:=Ps[player].x+1;
        Ps[player].y:=Ps[player].y;
      end;

    if Check(player, Ps[player].x, Ps[Player].y+1) then
      begin
        Ps[player].x:=Ps[player].x;
        Ps[player].y:=Ps[player].y+1;
        inc(Used[player, Ps[player].x, Ps[Player].y]);
        rekur(player mod 2 + 1, move);
        rekur(player, move+1);
        Ps[player].x:=Ps[player].x;
        Ps[player].y:=Ps[player].y-1;
      end;

    if Check(player, Ps[player].x, Ps[Player].y-1) then
      begin
        Ps[player].x:=Ps[player].x;
        Ps[player].y:=Ps[player].y-1;
        inc(Used[player, Ps[player].x, Ps[Player].y]);
        rekur(player mod 2 + 1, move);
        rekur(player, move+1);
        Ps[player].x:=Ps[player].x;
        Ps[player].y:=Ps[player].y+1;
      end;
{
    if Check(Ps[player].x, Ps[Player].y) then
      begin
        Ps[player].x:=Ps[player].x;
        Ps[player].y:=Ps[player].y;
        rekur(player mod 2 + 1, move+1);
        Ps[player].x:=Ps[player].x;
        Ps[player].y:=Ps[player].y;
      end;
}
    if Cool(k, Ps[player].x, Ps[player].y, player mod 2 + 1) then
      begin
        a[p[k].x1, p[k].y1]:=(a[p[k].x1, p[k].y1]+1) mod 2;
        rekur(player mod 2 + 1, move);
        rekur(player, move+1);
        a[p[k].x1, p[k].y1]:=abs(a[p[k].x1, p[k].y1]-1);
      end;

  end;
end;

Begin
assign(f, '');
reset(f);
readln(f, n, m);
for i:=1 to n do
  begin
    for j:=1 to m do
      read(f, a[i, j]);
    readln(f);
  end;
readln(f, pp);
for i:=1 to pp do
  readln(f, p[i].x, p[i].y, p[i].x1, p[i].y1);
for i:=1 to 2 do
  readln(f, Ps[i].x, Ps[i].y);

readln(f, x, y);
rekur(1, 1);

if max[1]>max[2] then
  writeln(2)
else
  if max[2]>max[1] then
    writeln(1)
  else
    writeln(0);

End.