Go: package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

var (
	dx = []int{0, -1, 0, 1}
	dy = []int{1, 0, -1, 0}
)

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		w, _ := strconv.Atoi(buf[0])
		h, _ := strconv.Atoi(buf[1])
		if w == 0 && h == 0 {
			break
		}
		scanner.Scan()
		buf_ := strings.Split(scanner.Text(), " ")
		xs, _ := strconv.Atoi(buf_[0])
		ys, _ := strconv.Atoi(buf_[1])
		scanner.Scan()
		buf__ := strings.Split(scanner.Text(), " ")
		xg, _ := strconv.Atoi(buf__[0])
		yg, _ := strconv.Atoi(buf__[1])
		xs--
		ys--
		xg--
		yg--
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		field := make([][]int, 100)
		for i := range  field {
			field[i] = make([]int, 100)
		}
		visited := make([][]bool, 100)
		for i := range visited {
			visited[i] = make([]bool, 100)
		}
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf___ := strings.Split(scanner.Text(), " ")
			c, _ := strconv.Atoi(buf___[0])
			d, _ := strconv.Atoi(buf___[1])
			x, _ := strconv.Atoi(buf___[2])
			y, _ := strconv.Atoi(buf___[3])
			x--
			y--
			switch d {
			case 0:
				for j := 0; j < 4; j++ {
					for k := 0; k < 2; k++ {
						field[x+j][y+k] = c
					}
				}
			case 1:
				for j := 0; j < 2; j++ {
					for k := 0; k < 4; k++ {
						field[x+j][y+k] = c
					}
				}
			}
		}
		if field[xs][ys] != field[xg][yg] || // スタートとゴールの色が等しくないと"NG"
			field[xs][ys] == 0 { // スタートの色が無職
			fmt.Println("NG")
			continue
		}
		Q := make([]Entry, 0)
		visited[xs][ys] = true
		Q = append(Q, Entry{xs, ys})
		for len(Q) > 0 {
			row := Q[0].x
			col := Q[0].y
			Q = Q[1:]
			for i := 0; i < 4; i++ {
				if 0 <= row+dx[i] && row+dx[i] < w &&
					0 <= col+dy[i] && col+dy[i] < h && // 移動可能な範囲内かどうか
					!visited[row + dx[i]][col + dy[i]] && // 訪問済みか
					field[row+dx[i]][col+dy[i]]==field[row][col] { // 色が同じかどうか
						visited[row+dx[i]][col+dy[i]] = true
						Q = append(Q, Entry{row+dx[i],col+dy[i]})
				}
			}
		}
		if visited[xg][yg] {
			fmt.Println("OK")
		} else {
			fmt.Println("NG")
		}
	}
}

type Entry struct {
	x, y int
}

C++: #include<stdio.h>
#include<string.h>
void haichi(void);
int Block(int,int);
int c,d,x,y,xg,yg,w,h,M,Flag;
int MAP[102][102],MF[102][102];
int main(void)
{
	int xs,ys,n;
	while(1){
		Flag=0;
		memset(MAP,0,sizeof(MAP));
		memset(MF,0,sizeof(MF));
		scanf("%d %d",&w,&h);
		if(w == 0 && h == 0) break;
		scanf("%d %d %d %d",&xs,&ys,&xg,&yg);
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			haichi();
		}
		/*for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				printf("%d",MAP[i][j]);
			}
			printf("\n");
		}*/
		M = MAP[ys][xs];//
		if(Block(xs,ys))
			printf("OK\n");
		else
			printf("NG\n");
	}
	return 0;
}

void haichi(void)
{
	if(d==0){
		MAP[y][x]=c;MAP[y][x+1]=c;MAP[y][x+2]=c;MAP[y][x+3]=c;
		MAP[y+1][x]=c;MAP[y+1][x+1]=c;MAP[y+1][x+2]=c;MAP[y+1][x+3]=c;
	}
	else if(d==1){
		MAP[y][x]=c;MAP[y][x+1]=c;
		MAP[y+1][x]=c;MAP[y+1][x+1]=c;
		MAP[y+2][x]=c;MAP[y+2][x+1]=c;
		MAP[y+3][x]=c;MAP[y+3][x+1]=c;
	}
}

int Block(int X,int Y)
{
	
	//printf("%d",M);
	//printf("%d %d\n",X,Y);
	if(X == xg && Y == yg)
		return 1;
	if(MAP[Y][X] != M || MF[Y][X] == 1 || X < 1 || Y < 1 || X > w || Y > h) return 0;
	MF[Y][X] = 1;
	return Block(X,Y-1) || Block(X,Y+1) || Block(X-1,Y) || Block(X+1,Y);
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
#include <iomanip>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int dx[] = {0, 1, 0, 1}, dy[] = {0, 0, 1, 1};
struct P{ int x, y; };
int main()
{
	int w, h;
	while(cin >> w >> h && (w || h))
	{
		vector<vint> field(w, vint(h, 0));
		P s, g;
		cin >> s.x >> s.y >> g.x >> g.y;
		s.x--, s.y--, g.x--, g.y--;

		int n;
		cin >> n;
		rep(i, n)
		{
			int c, d, x, y;
			cin >> c >> d >> x >> y; x--, y--;

			rep(i, 4)
				field[x + dx[i]][y + dy[i]] = c;
			rep(i, 4)
				field[x + dx[i] + 2 * !d][y + dy[i] + 2 * d] = c;
		}

		
		int c = field[s.x][s.y];
		/*
		if(!c || !field[g.x][g.y])
		{
			cout << "NG" << endl;
			continue;
		}
		*/

		rep(i, h) rep(j, w)
			cout << field[j][i] << (j + 1 == w ? "\n" : " ");

		queue<P> que;
		for(que.push(s); !que.empty(); que.pop())
		{
			P p = que.front();

			rep(i, 4)
			{
				int nx = p.x + dx[i], ny = p.y + dy[i];
				
				if(0 <= nx && nx < w && 0 <= ny && ny < h && field[nx][ny] == c)
				{
					field[nx][ny] = -1;
					P next; next.x = nx, next.y = ny;
					que.push(next);
				}
			}
		}

		cout << (field[g.x][g.y] == -1? "OK" : "NG") << endl;
	}
}

C++: #include <iostream>
#include <queue>
#include <map>

using namespace std;

typedef pair<int, int> P;

int B[101][101];

bool bfs(int w, int h, int xs, int ys, int xg, int yg);

int main(int argc, char** argv)
{
	int w, h;
	int xs, ys;
	int xg, yg;
	int n;
	int c, d, x, y;

	while( 1 )
	{
		cin >> w;
		cin >> h;
		if(w==0 && h==0) break;
		
		cin >> xs;
		cin >> ys;
		cin >> xg;
		cin >> yg;
		cin >> n;

		for(int i=0; i<n; i++)
		{
			cin >> c; cin >> d; cin >> x; cin >> y;
			if(d==0)
			{
				for(int dx=0; dx<4; dx++)
				{
					for(int dy=0; dy<2; dy++)
					{
						B[x+dx][y+dy] = c;
					}
				}
			}
			else
			{
				for(int dx=0; dx<2; dx++)
				{
					for(int dy=0; dy<4; dy++)
					{
						B[x+dx][y+dy] = c;
					}
				}
			}
		}

		cout << (bfs(w, h, xs, ys, xg, yg) ? "OK" : "NG") << endl;
	}

	return 0;
}

bool bfs(int w, int h, int xs, int ys, int xg, int yg)
{
	int x, y, c, xx, yy;
	int offset[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
	queue<P> q;
	P p;

	c = B[xs][ys];
	B[xs][ys] = 0;
	p.first = xs, p.second = ys;
	
	q.push(p);
	while(!q.empty())
	{
		p = q.front();
		q.pop();
		x = p.first;
		y = p.second;
		if(x==xg && y==yg) return true;
		B[x][y] = 0;

		for(int i=0; i<4; i++)
		{
			xx = x + offset[i][0];
			yy = y + offset[i][1];
			if(x>=0 && y>=0 && x<w && y<h)
			{
				if(B[xx][yy] == c)
				{
					p.first = xx;
					p.second = yy;
					q.push(p);
				}
			}
		}
	}

	return false;
}