/*
TASK:lab101
LANG:C
*/

#include <stdio.h>
#include <string.h>

short N, M, field[10][10], buttons[10][10][3], px[2], py[2], winx, winy,
      field1[2][10][10], queue[100000][4];
long qf=0, ql=0;

void add(short x, short y, short t, short p)
{
  queue[ql][0]=x;
  queue[ql][1]=y;
  queue[ql][3]=t;
  queue[ql++][2]=p;
}

int get(short *x, short *y, short *t, short *p)
{
  if(ql==qf)
    return 0;
  *x=queue[qf][0];
  *y=queue[qf][1];
  *t=queue[qf][3];
  *p=queue[qf++][2];
  return 1;
}

void input(void)
{
  short x, y, p, i;
  memset(buttons, 0, 600);
  scanf("%hd %hd", &N, &M);
  for(x=0; x<N; ++x)
    for(y=0; y<M; ++y)
    {
      scanf("%hd", &field[x][y]);
    }
  scanf("%hd", &p);
  for(i=0; i<p; ++i)
  {
    scanf("%hd %hd", &x, &y);
    --x;
    --y;
    scanf("%hd %hd", &buttons[x][y][0], &buttons[x][y][1]);
    buttons[x][y][0]--;
    buttons[x][y][1]--;
    buttons[x][y][2]=1;
  }
  scanf("%hd %hd %hd %hd %hd %hd", &px[0], &py[0], &px[1], &py[1], &winx, &winy);
  px[0]--; py[0]--; px[1]--; py[1]--; winx--; winy--;
}

short other(short i)
{
  if(i==1)
    return 0;
  return 1;
}

int main(void)
{
  short x, y, p, t;
  input();
  for(x=0; x<N; ++x)
    for(y=0; y<M; ++y)
      field1[0][x][y]=field1[1][x][y]=field[x][y];
  field1[0][px[0]][py[0]]=2;
  field1[1][px[1]][py[1]]=2;
  add(px[0], py[0], 2, 0);
  add(px[1], py[1], 2, 1);
  if(buttons[px[0]][py[0]][2])
    field1[1][buttons[px[0]][py[0]][0]][buttons[px[0]][py[0]][1]]=1;
  if(buttons[px[1]][py[1]][2])
    field1[0][buttons[px[1]][py[1]][0]][buttons[px[1]][py[1]][1]]=1;
  while(get(&x, &y, &t, &p))
  {
    if(x&& !field1[p][x-1][y])
    {
      field1[p][x-1][y]=t+1;
      if(buttons[x-1][y][2]&&!field1[other(p)][buttons[x-1][y][0]][buttons[x-1][y][1]])
        field1[other(p)][buttons[x-1][y][0]][buttons[x-1][y][1]]=1;
      if(buttons[x-1][y][2]&&field1[p][buttons[x-1][y][0]][buttons[x-1][y][1]]==1)
        field1[p][buttons[x-1][y][0]][buttons[x-1][y][1]]=0;
      add(x-1, y, t+1, p);
    }
    if(y&& !field1[p][x][y-1])
    {
      field1[p][x][y-1]=t+1;
      if(buttons[x][y-1][2]&&!field1[other(p)][buttons[x][y-1][0]][buttons[x][y-1][1]])
        field1[other(p)][buttons[x][y-1][0]][buttons[x][y-1][1]]=1;
      if(buttons[x][y-1][2]&&field1[p][buttons[x][y-1][0]][buttons[x][y-1][1]]==1)
        field1[p][buttons[x][y-1][0]][buttons[x][y-1][1]]=0;
      add(x, y-1, t+1, p);
    }
    if(x<N-1&& !field1[p][x+1][y])
    {
      field1[p][x+1][y]=t+1;
      if(buttons[x+1][y][2]&&!field1[other(p)][buttons[x+1][y][0]][buttons[x+1][y][1]])
        field1[other(p)][buttons[x+1][y][0]][buttons[x+1][y][1]]=1;
      if(buttons[x+1][y][2]&&field1[p][buttons[x+1][y][0]][buttons[x+1][y][1]]==1)
        field1[p][buttons[x+1][y][0]][buttons[x+1][y][1]]=0;
      add(x+1, y, t+1, p);
    }
    if(y<M-1&& !field1[p][x][y+1])
    {
      field1[p][x][y+1]=t+1;
      if(buttons[x][y+1][2]&&!field1[other(p)][buttons[x][y+1][0]][buttons[x][y+1][1]])
        field1[other(p)][buttons[x][y+1][0]][buttons[x][y+1][1]]=1;
      if(buttons[x][y+1][2]&&field1[p][buttons[x][y+1][0]][buttons[x][y+1][1]]==1)
        field1[p][buttons[x][y+1][0]][buttons[x][y+1][1]]=0;
      add(x, y+1, t+1, p);
    }
  }
  if(field1[0][winx][winy]>1)
  {
    if(field1[1][winx][winy]>1)
      if(field1[1][winx][winy]<field1[0][winx][winy])
        printf("2\n");
      else
        printf("1\n");
    else
      printf("1\n");
    return 0;
  }
  if(field1[1][winx][winy]>1)
    printf("2\n");
  else
    printf("0\n");
  return 0;
}
