#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
bool solve(int x,int y,int c);
int ux[2][8]={{0,1,2,3,0,1,2,3},
	      {0,1,0,1,0,1,0,1}};
int uy[2][8]={{0,0,0,0,1,1,1,1},
	      {0,0,1,1,2,2,3,3}};
int dx[4]={0,1,0,-1};
int dy[4]={-1,0,1,0};
int w,h,n;
int b[105][105];
int xs,ys,xg,yg;
int main(void){
  while(1){
    scanf("%d %d",&w,&h);
    if(!w && !h) break;
    memset(b,0,sizeof(b));
    scanf("%d %d %d %d",&xs,&ys,&xg,&yg);
    scanf("%d",&n);
    int c,d,tx,ty;
    for(int i=0;i<n;i++){
      scanf("%d %d %d %d",&c,&d,&tx,&ty);
      for(int j=0;j<8;j++){
	b[tx+ux[d][j]][ty+uy[d][j]]=c;
      }
    }
    if(b[xs][ys]!=0 && b[xg][yg]!=0 && b[xs][ys]==b[xg][yg] && solve(xs,ys,b[xs][ys])) puts("OK");
    else puts("NG");
  }
}

bool solve(int x,int y,int c){
  if(x==xg && y==yg) return true;
  b[x][y]=0;
  for(int i=0;i<4;i++){
    int nx=x+dx[i];
    int ny=y+dy[i];
    if(0<nx && nx<=w && 0<ny && ny<=h){
      if(b[nx][ny]==c){
	if(solve(nx,ny,c)) return true;
      }
    }
  }
  return false;
}
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
using ll = long long;
using ii = pair<int, int>;

string S[2002];
int P[2002][2002], A[2002][2002], B[2002][2002];

int main() {
    if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout);
    ios_base::sync_with_stdio(0), cin.tie(0);

    int N, M, Q; cin >> N >> M >> Q;
    for (int i = 0; i < N; ++i) cin >> S[i];

    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
            P[i+1][j+1] = P[i+1][j]+P[i][j+1]-P[i][j]+(S[i][j]=='1');
            if (i) A[i+1][j+1] = A[i+1][j]+A[i][j+1]-A[i][j]+(S[i][j]=='1'&&S[i-1][j]=='1');
            if (j) B[i+1][j+1] = B[i+1][j]+B[i][j+1]-B[i][j]+(S[i][j]=='1'&&S[i][j-1]=='1');
        }
    }
    
    while (Q--) {
        int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
        cout << P[x2][y2]-P[x1-1][y2]-P[x2][y1-1]+P[x1-1][y1-1]
               -A[x2][y2]+A[x1  ][y2]+A[x2][y1-1]-A[x1  ][y1-1]
               -B[x2][y2]+B[x1-1][y2]+B[x2][y1  ]-B[x1-1][y1  ] << '\n';
    }
}