{
TASK: lab101
LANG: PASCAL
}
const
 maxn = 11;
 maxk = 11;
 maxx = 1 shl 11;
 maxp = 65;
type
 cell = record i,j,x,y : longint; end;
var
 a : array[0..maxn,0..maxn] of longint;
 b,c,d : array[0..maxn,0..maxn] of longint;
 key : array[0..maxp] of cell;
 h   : array[0..maxn,0..maxn,0..maxn,0..maxn,0..maxx] of byte;
 i,j,m,n,p,fsti,fstj,seci,secj,wi,wj,k,x,max : longint;
procedure init;
var
 f : text;
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,p);
   k:=-1;
   for i:=1 to p do
   begin
      readln(f,key[i].i,key[i].j,key[i].x,key[i].y);
      if b[key[i].x,key[i].y]=0 then
      begin
         inc(k);
         b[key[i].x,key[i].y]:=k;
         c[key[i].x,key[i].y]:=1 shl k;
      end;
      d[key[i].i,key[i].j]:=c[key[i].x,key[i].y];
   end;
   readln(f,fsti,fstj);
   readln(f,seci,secj);
   readln(f,wi,wj);

end;
function move(fi,fj,si,sj : byte; k : longint) : byte;
var p , cur : longint;
begin
   if h[fi,fj,si,sj,k]>0 then
   begin
      move:=h[fi,fj,si,sj,k];
   end
   else
   if (fi=wi)and(fj=wj) then
   begin

      move:=1;
   end
   else
   if (si=wi)and(sj=wj) then
   begin

      move:=2;
   end
   else
   if k and max = 0 then
   begin
      h[fi,fj,si,sj,k]:=4;
      p:=0;
      cur:=0;
      if (fi>1)and(a[fi-1,fj]=0)and(h[fi-1,fj,si,sj,k]<>4) then
      begin
         if c[fi-1,fj]>0 then
         begin
            if not (x and c[fi-1,fj] = c[fi-1,fj]) then
             cur:=move(fi-1,fj,si,sj,k+max);
            if cur=1 then
            begin
               h[fi,fj,si,sj,k]:=1;
               move:=1;
               exit;
            end;
         end
         else
         begin
            cur:=move(fi-1,fj,si,sj,k+max);
            if cur=1 then
            begin
               h[fi,fj,si,sj,k]:=1;
               move:=1;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end;
      end;
      if (fi<n)and(a[fi+1,fj]=0)and(h[fi+1,fj,si,sj,k]<>4) then
      begin
         if c[fi+1,fj]>0 then
         begin
            if not (x and c[fi+1,fj] = c[fi+1,fj]) then
             cur:=move(fi+1,fj,si,sj,k+max);
            if cur=1 then
            begin
               h[fi,fj,si,sj,k]:=1;
               move:=1;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end
         else
         begin
            cur:=move(fi+1,fj,si,sj,k+max);
            if cur=1 then
            begin
               h[fi,fj,si,sj,k]:=1;
               move:=1;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end;
      end;
      {-------------------------------}

      if (fj<m)and(a[fi,fj+1]=0)and(h[fi,fj+1,si,sj,k]<>4) then
      begin
         if c[fi,fj+1]>0 then
         begin
            if not (x and c[fi,fj+1] = c[fi,fj+1]) then
             cur:=move(fi,fj+1,si,sj,k+max);
            if cur=1 then
            begin
               h[fi,fj,si,sj,k]:=1;
               move:=1;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end
         else
         begin
            cur:=move(fi,fj+1,si,sj,k+max);
            if cur=1 then
            begin
               h[fi,fj,si,sj,k]:=1;
               move:=1;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end;
      end;
      if (fj>1)and(a[fi,fj-1]=0)and(h[fi,fj-1,si,sj,k]<>4) then
      begin
         if c[fi,fj-1]>0 then
         begin
            if not (x and c[fi,fj-1] = c[fi,fj-1]) then
             cur:=move(fi,fj-1,si,sj,k+max);
            if cur=1 then
            begin
               h[fi,fj,si,sj,k]:=1;
               move:=1;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end
         else
         begin
            cur:=move(fi,fj-1,si,sj,k+max);
            if cur=1 then
            begin
               h[fi,fj,si,sj,k]:=1;
               move:=1;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end;
      end;
      {------------------------------------}
      if c[fi,fj]>0 then
      begin
         cur:=move(fi,fj,si,sj,x + c[fi,fj]);
         if cur = 1 then
         begin
            h[fi,fj,si,sj,k]:=cur;
            move:=1;
            exit;
         end;
      end;
      if (p=0)and(cur=0) then p:=1;
      if (p=0)and(cur=2) then p:=2;
      h[fi,fj,si,sj,k]:=p;
      move:=p;

   end
   else
   begin
      p:=0;
      cur:=0;
      h[fi,fj,si,sj,k]:=4;

      if (si>1)and(a[si-1,sj]=0)and(h[fi,fj,si-1,sj,k]<>4) then
      begin
         if c[si-1,sj]>0 then
         begin
            if not (x and c[si-1,sj] = c[si-1,sj]) then
             cur:=move(fi,fj,si-1,sj,k-max);
            if cur=2 then
            begin
               h[fi,fj,si,sj,k]:=2;
               move:=2;
               exit;
            end;
         end
         else
         begin
            cur:=move(fi,fj,si-1,sj,k-max);
            if cur=2 then
            begin
               h[fi,fj,si,sj,k]:=2;
               move:=2;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end;
      end;

      if (si<n)and(a[si+1,sj]=0)and(h[fi,fj,si+1,sj,k]<>4) then
      begin
         if c[si+1,sj]>0 then
         begin
            if not (x and c[si+1,sj] = c[si+1,sj]) then
             cur:=move(fi,fj,si+1,sj,k-max);
            if cur=2 then
            begin
               h[fi,fj,si,sj,k]:=2;
               move:=2;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end
         else
         begin
            cur:=move(fi,fj,si+1,sj,k-max);
            if cur=2 then
            begin
               h[fi,fj,si,sj,k]:=2;
               move:=2;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end;
      end;
      {------------------------------  }
      if (sj>1)and(a[si,sj-1]=0)and(h[fi,fj,si,sj-1,k]<>4) then
      begin
         if c[si,sj-1]>0 then
         begin
            if not (x and c[si,sj-1] = c[si,sj-1]) then
             cur:=move(fi,fj,si,sj-1,k-max);
            if cur=1 then
            begin
               h[fi,fj,si,sj,k]:=1;
               move:=1;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end
         else
         begin
            cur:=move(fi,fj,si,sj-1,k-max);
            if cur=2 then
            begin
               h[fi,fj,si,sj,k]:=2;
               move:=2;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end;
      end;
      {------------------------------------}
      if (sj<m)and(a[si,sj+1]=0)and(h[fi,fj,si,sj+1,k]<>4) then
      begin
         if c[si,sj+1]>0 then
         begin
            if not (x and c[si,sj+1] = c[si,sj+1]) then
             cur:=move(fi,fj,si,sj+1,k-max);
            if cur=2 then
            begin
               h[fi,fj,si,sj,k]:=2;
               move:=2;
               exit;
            end
            else
            if cur=3 then
            p:=3;
         end
         else
         begin
            cur:=move(fi,fj,si,sj+1,k-max);
            if cur=2 then
            begin
               h[fi,fj,si,sj,k]:=2;
               move:=2;
               exit;
            end
            else
            if cur=3 then
            p:=cur;
         end;
      end;
      if c[si,sj]>0 then
      begin
         cur:=move(fi,fj,si,sj,x + c[si,sj]);
         if cur = 2 then
         begin
            h[fi,fj,si,sj,k]:=cur;
            move:=2;
            exit;
         end;
      end;
      if (p=0)and(cur=0) then p:=2;
      if (p=0)and(cur=1) then p:=1;
      h[fi,fj,si,sj,k]:=p;
      move:=p;
   end;
end;
procedure outp;
var
 ans : longint;
begin
   max:=1 shl (k+1);
   for i:=1 to p do
   if a[key[i].x,key[i].y]=1 then
   if x and c[key[i].x,key[i].y] = 0 then
   begin
      x:=x+c[key[i].x,key[i].y];
   end;
   ans:=move(fsti,fstj,seci,secj,x);
   if ans = 3 then ans:=0;
   writeln(ans);
end;
begin
   init;
   outp;
   for i:=1 to n do
end.
