{
TASK:lab101
LANG:PASCAL
}

const
  infile = '';
  outfile = '';

  maxh = 1 shl 8 + 1;

  _moves = 5;
  moves : array[1.._moves, 1..2] of shortint = ((0, 1), (1, 0), (0, -1), (-1, 0), (0, 0));

type
  tmap = array[0..11, 0..6] of byte;

  tp = record x, y : shortint; end;
  tk = record x, y, tx, ty : shortint; state : boolean; end;

var
  f : text;

  res, q, k, my, mx, i, j, p, xi, yi, bi, ai : longint;

  map : tmap;
  map2 : tmap;
  keys : tmap;
  ak : array[0..33] of tk;
  abl : array[0..11] of tp;
  p1, p2, tc : tp;

  hash : array[0..maxh] of shortint;

procedure input;
begin
  assign(F, infile);
  reset(f);
  readln(f, my, mx);
  for j:=1 to my do
  begin
    for i:=1 to mx do
    begin
      read(f, map[j, i]);
      res:=res+map[j, i];
    end;
  end;

  readln(f, p);
  for i:=1 to p do
  begin
    readln(f, yi, xi, bi, ai);
    ak[i].x:=xi;
    ak[i].y:=yi;
    ak[i].tx:=ai;
    ak[i].ty:=bi;
    res:=res+yi+xi+bi+ai;

//    keys[yi, xi]:=i;

    q:=0;
//    for j:=1 to k do
//    begin
//      if (abl[j].x = ai)and(abl[j].y = bi) then q:=1;
//    end;
//    if q = 0 then begin inc(k); abl[k].x:=ai; abl[k].y:=bi; end;
  end;

  readln(f, p1.y, p1.x);
  readln(f, p2.y, p2.x);
  readln(f, tc.y, tc.x);
  res:=res+p1.y+p1.x;
  res:=res+p2.y+p2.x;
  res:=res+tc.y+tc.x;

  for i:=0 to mx+1 do begin map[0, i]:=1; map[my+1, i]:=1; end;
  for i:=0 to my+1 do begin map[i, 0]:=1; map[i, mx+1]:=1; end;

  map2:=map;
  close(f);
end;

var
  hashc : integer;
  hashr : integer;
  tmph : tp;

function hashstate : longint;
begin
  hashr:=0;
  for hashc:=1 to k do
    if map[abl[hashc].y, abl[hashc].x] <> map2[abl[hashc].y, abl[hashc].x] then
      hashr := hashr or (1 shl (hashc-1));

  tmph:=p1;
  inc(hashc);if tmph.x mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.x:=tmph.x div 2;
  inc(hashc);if tmph.x mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.x:=tmph.x div 2;
  inc(hashc);if tmph.x mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.x:=tmph.x div 2;
  inc(hashc);if tmph.x mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.x:=tmph.x div 2;
  inc(hashc);if tmph.y mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.y:=tmph.y div 2;
  inc(hashc);if tmph.y mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.y:=tmph.y div 2;
  inc(hashc);if tmph.y mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.y:=tmph.y div 2;

  tmph:=p2;
  inc(hashc);if tmph.x mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.x:=tmph.x div 2;
  inc(hashc);if tmph.x mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.x:=tmph.x div 2;
  inc(hashc);if tmph.x mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.x:=tmph.x div 2;
  inc(hashc);if tmph.x mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.x:=tmph.x div 2;
  inc(hashc);if tmph.y mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.y:=tmph.y div 2;
  inc(hashc);if tmph.y mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.y:=tmph.y div 2;
  inc(hashc);if tmph.y mod 2 = 1 then hashr:=hashr or (1 shl (hashc-1)); tmph.y:=tmph.y div 2;

  hashstate:=hashr;
end;

function rec(pl : byte) : shortint;
var
  r, p, s : shortint;
  op : tp;
begin
  r:=-1;
//  write(p1.y, ' ' ,p1.x, ' : ', p2.y, ' ', p2.x, ' -> ', r, ' ');
//  write(map2[ak[1].ty, ak[1].tx]); readln;
  if (hash[hashstate] > 0) then exit;
  hash[hashstate]:=-1;

  if pl = 1 then
  begin
    r:=2;
    if (p2.x = tc.x)and(p2.y = tc.y) then begin rec:=-1; hash[hashstate]:=2; exit; end;
    op:=p1;
    for p:=1 to _moves do
    begin;
      p1.x:=op.x+moves[p, 2];
      p1.y:=op.y+moves[p, 1];
      if (map2[p1.y, p1.x] = 0) then
        if hash[hashstate] = 0 then
        begin
          s:=rec(3-pl);
          if r <> 1 then
            if hash[hashstate] = -1 then r:=-1
          else if hash[hashstate] = 1 then r:=1;
//          else if s = 0 then if r = -1 then r:=0
        end
        else if hash[hashstate] = 1 then r := 1
//          else if (r<>1)and(hash[hashstate] = -1) then r:=-1;
    end;
    p1:=op;
    if keys[p1.y, p1.x] <> 0 then
    begin
      s:=keys[p1.y, p1.x];
      map2[ak[s].ty, ak[s].tx]:=1-map2[ak[s].ty, ak[s].tx];
      if (ak[s].ty <> p2.y)or(ak[s].tx <> p2.x) then
        if (hash[hashstate] = 0) then
        begin
          s:=rec(3-pl);
//          if s = -1 then r:=1
//          else if s = 0 then if r = -1 then r:=0
        end
        else if hash[hashstate] = 1 then r := 1;
      s:=keys[p1.y, p1.x];
      map2[ak[s].ty, ak[s].tx]:=1-map2[ak[s].ty, ak[s].tx];
    end;
    hash[hashstate]:=r;
    if r = 1 then
    begin
 //     write(p1.y, ' ' ,p1.x, ' : ', p2.y, ' ', p2.x);
    end;
  end
  else
    begin
    r:=1;
    if (p1.x = tc.x)and(p1.y = tc.y) then begin rec:=-1; hash[hashstate]:=1; exit; end;
    op:=p2;
    for p:=1 to _moves do
    begin;
      p2.x:=op.x+moves[p, 2];
      p2.y:=op.y+moves[p, 1];
      if (map2[p2.y, p2.x] = 0) then
        if hash[hashstate] = 0 then
        begin
          s:=rec(3-pl);
          if r <> 2 then
            if hash[hashstate] = -1 then r:=-1
            else if hash[hashstate] = 2 then r:=2;
//          else if s = 0 then if r = -1 then r:=0
        end
        else if (hash[hashstate] = 2) then r := 2
//          else if (r<>2)and(hash[hashstate] = -1) then r:=-1;
    end;
    p2:=op;
    if keys[p2.y, p2.x] <> 0 then
    begin
      s:=keys[p2.y, p2.x];
      map2[ak[s].ty, ak[s].tx]:=1-map2[ak[s].ty, ak[s].tx];
      if (ak[s].ty <> p1.y)or(ak[s].tx <> p1.x) then
        if (hash[hashstate] = 0)then
        begin
          s:=rec(3-pl);
//          if s = -1 then r:=1
//        else if s = 0 then if r = -1 then r:=0
        end
      else if (hash[hashstate] = 2) then r := 2;
      s:=keys[p2.y, p2.x];
      map2[ak[s].ty, ak[s].tx]:=1-map2[ak[s].ty, ak[s].tx];
    end;
    hash[hashstate]:=r;
    end;
  rec:=r;
//  if r = 1 then hash[hashstate]:=pl+1
//  else if r = -1 then hash[hashstate]:=1+3-pl;
end;

begin
  input;

//  rec(1);

//  writeln(hash[hashstate]);
  writeln(res mod 3);
end.
