/*
TASK: lab101
LANG: C
*/
#include <stdio.h>
#include <string.h>

 typedef
  struct
   {
    int x1,y1,x2,y2,mask;
   } pos;

 typedef
  struct
   {
    int x,y;
   } point;

 pos tmp;
 int n,m,p,cnt;
 int used[16][16];
 char a[14992384];
 point list[64];
 char marked[14992384];
 char reach[14992384];
 int s,e;
 int q[10240002];
 int coef[8];
 int o[16][16];
 int z[16][16];
 point b[16][16];
 int X1,Y1,X2,Y2,X,Y;
 int move[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

 void relax (int j,int i)
  {
   if (marked[j]) return;
   if (a[i]==-1)
    if (marked[j]==0)
     {
      a[j]=1;
      q[++e]=j;
      marked[j]=1;
     }
   if (a[i]==1)
    {
     reach[j]--;
     if (reach[j]==0)
      {
       a[j]=-1;
       q[++e]=j;
       marked[j]=1;
      }
    }
  }

 int code (int x1,int y1,int x2,int y2,int mask)
  {
   int ret=0;
   ret+=coef[0]*x1;
   ret+=coef[1]*y1;
   ret+=coef[2]*x2;
   ret+=coef[3]*y2;
   ret+=coef[4]*mask;
   return ret;
  } 

 pos decode (int i)
  {
   pos ret;
   ret.mask=i/coef[4];i%=coef[4];
   ret.y2=i/coef[3];i%=coef[3];
   ret.x2=i/coef[2];i%=coef[2];
   ret.y1=i/coef[1];i%=coef[1];
   ret.x1=i/coef[0];i%=coef[0];
   return ret;
  }

 int main ()
  {
   int i,j,v,u,k,w,pom;
   scanf("%d%d",&n,&m);
   coef[0]=1;
   coef[1]=(n+1)*coef[0];
   coef[2]=(m+1)*coef[1];
   coef[3]=(n+1)*coef[2];
   coef[4]=(m+1)*coef[3];
   for (i=1;i<=n;i++)
    for (j=1;j<=m;j++)
     {
      scanf("%d",&z[i][j]);
      z[i][j]^=1;
     }
   scanf("%d",&p);
   cnt=0;
   for (i=1;i<=p;i++)
    {
     scanf("%d%d",&v,&u);
     scanf("%d%d",&b[v][u].x,&b[v][u].y);
     if (o[b[v][u].x][b[v][u].y]==0)
      {
       list[cnt].x=b[v][u].x;
       list[cnt].y=b[v][u].y;      
       cnt++;
       o[b[v][u].x][b[v][u].y]=cnt;
      }
    }
   for (i=1;i<=n;i++)
    for (j=1;j<=m;j++)
     if (o[i][j]!=0) o[i][j]--;
   for (k=0;k<(1<<cnt);k++)
    {
     for (i=1;i<=n;i++)
      for (j=1;j<=m;j++)
       used[i][j]=z[i][j];
     for (i=0;i<cnt;i++)
      if ((k>>i)&1)
       used[list[i].x][list[i].y]^=1;
     for (X1=1;X1<=n;X1++)
      for (Y1=1;Y1<=m;Y1++)
       if (used[X1][Y1]==1)
        for (X2=1;X2<=n;X2++)
         for (Y2=1;Y2<=m;Y2++)
          if (used[X2][Y2]==1)
           {
            pom=code(X1,Y1,X2,Y2,k);
//stay            
            reach[pom]++;
//move
            for (w=0;w<4;w++)
             if (used[X1+move[w][0]][Y1+move[w][1]]==1)
              reach[pom]++;
//swap
            if (b[X1][Y1].x+b[X1][Y1].y!=0
             && (b[X1][Y1].x!=X1 || b[X1][Y1].y!=Y1)
             && (b[X1][Y1].x!=X2 || b[X1][Y1].y!=Y2))
              reach[pom]++;
           }
    }
   scanf("%d%d%d%d%d%d",&X1,&Y1,&X2,&Y2,&X,&Y);
   for (k=0;k<(1<<cnt);k++)
    {
     for (i=1;i<=n;i++)
      for (j=1;j<=m;j++)
       used[i][j]=z[i][j];
     for (i=0;i<cnt;i++)
      if ((k>>i)&1)
       used[list[i].x][list[i].y]^=1;
     for (i=1;i<=n;i++)
      for (j=1;j<=m;j++)
       if ((i!=X || j!=Y)
       && used[i][j]+used[X][Y]==2)
        {
         pom=code(i,j,X,Y,k);
         q[++e]=pom;
         marked[pom]=1;
         a[pom]=-1;
//
         pom=code(X,Y,i,j,k);
         q[++e]=pom;
         marked[pom]=1;
         a[pom]=1;
        }
    }
   for (s=1;s<=e;s++)
    {
     tmp=decode(q[s]);
     for (i=1;i<=n;i++)
      for (j=1;j<=m;j++)
       used[i][j]=z[i][j];
     for (i=0;i<cnt;i++)
      if ((tmp.mask>>i)&1)
       used[list[i].x][list[i].y]^=1;
// move
     for (i=0;i<4;i++)
      if (used[tmp.x2+move[i][0]][tmp.y2+move[i][1]]==1)
       {
        j=code(tmp.x2+move[i][0],tmp.y2+move[i][1],tmp.x1,tmp.y1,tmp.mask);
        relax(j,q[s]);
       }
// swap
     if (b[tmp.x2][tmp.y2].x+b[tmp.x2][tmp.y2].y!=0
     && (b[tmp.x2][tmp.y2].x!=tmp.x1 || b[tmp.x2][tmp.y2].y!=tmp.y1)
     && (b[tmp.x2][tmp.y2].x!=tmp.x2 || b[tmp.x2][tmp.y2].y!=tmp.y2))
       {
        j=code(tmp.x2,tmp.y2,tmp.x1,tmp.y1,(tmp.mask^(1<<o[tmp.x2][tmp.y2])));
        relax(j,q[s]);
       }
// stay
     j=code(tmp.x2,tmp.y2,tmp.x1,tmp.y1,tmp.mask);
     relax(j,q[s]);
    }
   i=code(X1,Y1,X2,Y2,0);
   if (a[i]==1) printf("1\n");
   if (a[i]==-1) printf("2\n");
   if (a[i]==0) printf("0\n");
   return 0;
  }
