#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

int xg,yg,c,a[1000][1000],memo[1000][1000],ans,p,w,h;

int solve(int ys,int xs){

  if(memo[ys][xs] < 0) return memo[ys][xs];

  if(ys + 1 <= w && a[ys+1][xs] == p) solve(ys+1,xs);
  else if(xs + 1 <= h && a[ys][xs+1] == p) solve(ys,xs+1);
  //else if(ys - 1 > 0 /*&& a[ys-1][xs] == p*/) solve(ys-1,xs);
  //else if(xs - 1 > 0 && a[ys][xs-1] == p) solve(ys,xs-1);

  if(xs == xg && ys == yg) return ans = 1;

  return  memo[ys][xs] = -1;
}

int main(){
  int xs,ys,n,d,x,y;

  while(true){
    cin >> w >> h;
    if(!w && !h) break;

    memset(a,0,sizeof(a));
    memset(memo,0,sizeof(memo));
    ans = 0;

    cin >> xs >> ys >> xg >> yg >> n;

    for(int i=0;i<n;i++){
      cin >> c >> d >> x >> y;

      if(d == 0){
	for(int i=y;i<y+2;i++){
	  for(int j=x;j<x+4;j++) a[i][j] = c;
	}
      }

      else if(d == 1){
	for(int i=y;i<y+4;i++){
	  for(int j=x;j<x+2;j++) a[i][j] = c;
	}
      }
    }

    p = a[yg][xg];
    solve(ys,xs);

    for(int i=1;i<=h;i++){
      cout << endl;
      for(int j=1;j<=w;j++) cout << a[i][j];
    }

    if(ans == 1)   cout << "OK" << endl;
    else if(ans == 0)cout << "NG" << endl;

  }
}