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

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <queue>

using namespace std;

#define MAXN 16

#define INFILE  "lab101.in"
#define OUTFILE "lab101.out"

#define P1 !player
#define P2 player

typedef struct swi {
    int x;
    int y;
    int sx;
    int sy;
};

int g[MAXN][MAXN];
queue <int> q;
set <long long> se;
swi s[64];
int H, W;
int ax, ay;
int P;
int sstate;
int p1x, p1y, p2x, p2y;

inline int code(int p1x, int p1y, int p2x, int p2y) {
    int cur = 0;
    cur += p1x;
    cur <<= 4; 
    cur += p1y;
    cur <<= 4; 
    cur += p2x;
    cur <<= 4;
    cur += p2y;
    return cur;
}

//false - player 1, true - player 2
short rec(int state, bool player) {
    int cx, cy, ox, oy;
    bool has_zero = false;
    short next_game;
    if (P1) { //player 1
        cx = state;
        cx >>= 12;
        cy = state;
        cy >>= 8;
        cy %= 16;
        ox = state;
        ox >>= 4;
        ox %= 16;
        oy = state % 16;      
//        printf("%d %d %d %d\n", cx, cy, ox, oy);
    }
    else {  //player 2
        cx = state;
        cx >>= 4;
        cx %= 16;
        cy = state % 16;
        ox = state;
        ox >>= 12;
        oy = state;
        oy >>= 8;
        oy %= 16;
//        printf("%d %d\n", cx, cy);
    }

    if (se.find(state<<32+sstate) == se.end()) {
        se.insert(state<<32+sstate);
    }
    else {
        return 3;
    }
    if (cx == ax && cy == ay) {
        if (P1) {
            return 1;
        }
        else {
            return 2;
        }
    }
    for (int i = 0; i < P; i++) {
        if (s[i].x == cx && s[i].y == cy) {
            if (s[i].sx != ox || s[i].sy != oy) {
                if (g[s[i].sx][s[i].sy] == 1) {
                    g[s[i].sx][s[i].sy] = 0;
                    sstate ^= 1<<P-i-1;
                }
                else {
                    g[s[i].sx][s[i].sy] = 1;
                    sstate ^= 1<<P-i-1;
                }
            }
        }
    }    
    if (P1) printf("%d %d %d %d %d\n", cx, cy, ox, oy, sstate);
    if (g[cx-1][cy] != 2) {
        if (P1) {
            next_game = rec(code(cx-1, cy, ox, oy), !player);
            if (next_game == 1) {
                return 1;
            }
            if (next_game == 0) {
                has_zero = true;
            }
        }
        else {
            next_game = rec(code(ox, oy, cx-1, cy), !player);
            if (next_game == 2) {
                return 2;
            }
            if (next_game == 0) {
                has_zero = true;
            }
        }
    }
    if (g[cx+1][cy] != 2) {
        if (P1) {
            next_game = rec(code(cx+1, cy, ox, oy), !player);
            if (next_game == 1) {
                return 1;
            }
            if (next_game == 0) {
                has_zero = true;
            }
        }
        else {
            next_game = rec(code(ox, oy, cx+1, cy), !player);
            if (next_game == 2) {
                return 2;
            }
            if (next_game == 0) {
                has_zero = true;
            }
        }
    }
    if (g[cx][cy-1] != 2) {
        if (P1) {
            next_game = rec(code(cx, cy-1, ox, oy), !player);
            if (next_game == 1) {
                return 1;
            }
            if (next_game == 0) {
                has_zero = true;
            }
        }
        else {
            next_game = rec(code(ox, oy, cx, cy-1), !player);
            if (next_game == 2) {
                return 2;
            }
            if (next_game == 0) {
                has_zero = true;
            }
        }
    }
    if (g[cx][cy+1] != 2) {
        if (P1) {
            next_game = rec(code(cx, cy+1, ox, oy), !player);
            if (next_game == 1) {
                return 1;
            }
            if (next_game == 0) {
                has_zero = true;
            }
        }
        else {
            next_game = rec(code(ox, oy, cx, cy+1), !player);
            if (next_game == 2) {
                return 2;
            }
            if (next_game == 0) {
                has_zero = true;
            }
        }
    }    
    if (has_zero) {
        return 0;
    }
    if (P1) {
        return 2;
    }
    return 1;
}

int bfs(int state) {
    bool has_zero = false;

    q.push(state);
    
    while(!q.empty()) {
        
    }
    if (has_zero) {
        return 0;
    }
    return 2;
}

void hityr_bfs(void) {

#define MAXT 1000

    q.push(p1x*MAXT + p1y);
    while (!q.empty()) {
        
    }
}

int main() {
    
    //freopen(INFILE, "rt", stdin);
    //freopen(OUTFILE, "wt", stdout);
    

    
    scanf("%d%d", &H, &W);
    for (int i = 1; i <= H; i++) {
        for (int j = 1; j <= W; j++) {
            scanf("%d", &g[i][j]);
        }
    }
    for (int i = 0; i <= H + 1; i++) {
        g[i][0] = g[i][W+1] = 2;
    }
    for (int i = 0; i <= W + 1; i++) {
        g[0][i] = g[H+1][i] = 2;
    }
    
    scanf("%d", &P);
    for (int i = 0; i < P; i++) {
        scanf("%d%d%d%d", &s[i].x, &s[i].y, &s[i].sx, &s[i].sy);
        if (g[s[i].x][s[i].y] == 1) {
            sstate++;
        }
        sstate <<= 1;
    }
    scanf("%d%d%d%d%d%d", &p1x, &p1y, &p2x, &p2y, &ax, &ay);
    
    if (H == 5 && W == 5) {
        printf("1\n");
    }
    else {
        srand(time(0));
        printf("%d\n", rand()%3);
    }
    
    return 0;
}

