/*
TASK: lab101
LANG: C++
*/

#include <stdio.h>

struct cell { int x, y, num; };

const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};

int n, m, p;
int A[12][12];
cell button[12][12];
int pos[12][12][12][12][1025];

cell first, second, end;

void input ()
{
   int i, j, a, b;

   scanf ("%d%d", &n, &m);

   for (i=1; i<=n; i++)
      for (j=1; j<=m; j++)
         scanf ("%d", &A[i][j]);

   scanf ("%d", &p);

   for (i=0; i<p; i++) {
      scanf ("%d%d", &a, &b);
      scanf ("%d%d", &button[a][b].x, &button[a][b].y);
      button[a][b].num = i;
   }

   scanf ("%d%d", &first.x, &first.y);
   scanf ("%d%d", &second.x, &second.y);
   scanf ("%d%d", &end.x, &end.y);
}

void init ()
{
   int i, q1, q2, q3, q4, q5;

   for (i=0; i<=n+1; i++)
      A[i][0] = A[i][m+1] = 1;

   for (i=0; i<=m+1; i++)
      A[0][i] = A[n+1][i] = 1;

   for (q1=1; q1<=n; q1++)
      for (q2=1; q2<=m; q2++)
         for (q3=1; q3<=n; q3++)
            for (q4=1; q4<=m; q4++)
               for (q5=0; q5<p; q5++)
                  pos[q1][q2][q3][q4][q5] = 9;
}

// 0 - gube6ta za (x1,y1)
// 1 - pe4eliv6a za (x1,y1)
// 3 - zaciklena = neutralna
int solve (int x1, int y1, int x2, int y2, int conf)
{
   if (pos[x1][y1][x2][y2][conf] < 5) return pos[x1][y1][x2][y2][conf];
   if (end.x==x2 && end.y==y2) return 0;

   pos[x1][y1][x2][y2][conf] = 3;
   int i, res=0, p;

   for (i=0; i<4; i++)
      if (A[ x1+dx[i] ][ y1+dy[i] ] == 0) {
         p = solve (x2, y2, x1+dx[i], y1+dy[i], conf);
         if (p==0) return (pos[x1][y1][x2][y2][conf]=1);
//         else if (p==3) res = 3;
      }

   if (button[x1][y1].x && (x2!=button[x1][y1].x || y2!=button[x1][y1].y)) {
      A[ button[x1][y1].x ][ button[x1][y1].y ] ^= 1;
      p = solve(x2,y2,x1,y1,conf^(1<<button[x1][y1].num));
      if (p==0) return (pos[x1][y1][x2][y2][conf]=1);
//      else if (p==3) res = 3;
      A[ button[x1][y1].x ][ button[x1][y1].y ] ^= 1;
   }

   p = solve(x1,y1,x2,y2,conf);
   if (p==0) res = 1;
//   else if (p==3) res = 3;

   pos[x1][y1][x2][y2][conf] = res;
   return res;
}

int main ()
{
   //freopen ("lab101.in", "r", stdin);

   input ();
   init ();

   int p = solve (first.x, first.y, second.x, second.y, 0);
   if (p==1) printf ("1\n");
   if (p==2) printf ("2\n");
   if (p==3) printf ("0\n");

   return 0;
}