#include<cstdio>
#include<queue>
using namespace std;
int fx[4]={1,-1,0,0},fy[4]={0,0,1,-1};
int main(void)
{
	int **board;
	int bx,by;
	int sx,sy;
	int gx,gy;
	int bnum;
	while(1)
	{
		scanf("%d %d",&bx,&by);
		if(bx==0&&by==0)
		{
			break;
		}
		board=new int*[by];
		for(int f1=0;f1<by;f1++)
		{
			board[f1]=new int[bx];
			for(int f2=0;f2<bx;f2++)
			{
				board[f1][f2]=0;
			}
		}
		scanf("%d %d",&sx,&sy);
		scanf("%d %d",&gx,&gy);
		scanf("%d",&bnum);
		for(int f1=0;f1<bnum;f1++)
		{
			int c,d,x,y;
			scanf("%d %d %d %d",&c,&d,&x,&y);
			x--;y--;
			if(!d)
			{
				for(int f2=0;f2<2;f2++)
				{
					for(int f3=0;f3<4;f3++)
					{
						board[y+f2][x+f3]=c;
					}
				}
			}
			else
			{
				for(int f2=0;f2<4;f2++)
				{
					for(int f3=0;f3<2;f3++)
					{
						board[y+f2][x+f3]=c;
					}
				}
			}
		}
		for(int f1=0;f1<by;f1++)
		{
			for(int f2=0;f2<bx;f2++)
			{
				printf("%d",board[f1][f2]);
			}
			printf("\n");
		}
		int st_col=board[sy-1][sx-1];
		int gl_col=board[gy-1][gx-1];
		if(st_col!=gl_col||st_col==0||gl_col==0)
		{
			printf("NG\n");
			delete []board;
			continue;
		}
		queue<pair<int,int> > que;
		que.push(make_pair(sy,sx));
		int flg=0;
		while(!que.empty())
		{
			pair<int,int> buf=que.front();que.pop();
			if(buf.first==gy-1&&buf.second==gx-1)
			{
				printf("OK\n");
				delete []board;
				flg=1;
				break;
			}
			board[buf.first][buf.second]=9;
			for(int f1=0;f1<4;f1++)
			{
				if(buf.first+fy[f1]>=0&&buf.first+fy[f1]<by&&
					buf.second+fx[f1]>=0&&buf.second+fx[f1]<bx)
				{
					if(board[buf.first+fy[f1]][buf.second+fx[f1]]==st_col)
					{
						que.push(make_pair(buf.first+fy[f1],buf.second+fx[f1]));
					}
				}
			}
		}
		if(flg)
		{
			continue;
		}
		printf("NG\n");
		/*
		for(int f1=0;f1<by;f1++)
		{
			for(int f2=0;f2<bx;f2++)
			{
				printf("%d",board[f1][f2]);
			}
			printf("\n");
		}
		*/
		delete []board;
	}
}