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<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool N[102][102];
int map[102][102],R,ga,gb,w,h,f=0;
void block(int ,int);
int main()
{
	int i,n,c,d,X=0,Y=0,x,y;
	cin>>w>>h;
	while(w!=0||h!=0){
		memset(map,0,sizeof(map));
		memset(N,false,sizeof(N));
		cin>>X>>Y;
		cin>>ga>>gb;
		cin>>n;
		for(i=0;i<n;i++){
			cin>>c>>d>>x>>y;
			if(d){
				map[y][x]=c;
				map[y+1][x]=c;
				map[y+2][x]=c;
				map[y+3][x]=c;
				map[y][x+1]=c;
				map[y+1][x+1]=c;
				map[y+2][x+1]=c;
				map[y+3][x+1]=c;

			}
			else{
				map[y][x]=c;
				map[y+1][x+1]=c;
				map[y+1][x+2]=c;
				map[y+1][x+3]=c;
				map[y+1][x]=c;
				map[y][x+1]=c;
				map[y][x+2]=c;
				map[y][x+3]=c;

			}
		}
		R=map[Y][X];
		if(R==map[gb][ga]&&R!=0){
			block(Y,X);
			if(f==1) cout<<"OK"<<endl;
		        else cout<<"NG"<<endl;
		}
		else cout<<"NG"<<endl;
		//for(i=0;i<h;i++){
			//for(j=0;j<w;j++){
			//	cout<<map[i][j];
			//}
				
	//		cout<<endl;
		//}
		f=0;
		cin>>w>>h;
	}
	return 0;
}
void block(int b,int a)
{
	//if(f==1) return 1;
	N[b][a]=true;
	if(ga==a&&gb==b) f=1;
		if(map[b][a+1]==R&&N[b][a+1]==false) block(b,a+1); //???
		if(map[b+1][a]==R&&N[b+1][a]==false) block(b+1,a); //???
		if(map[b][a-1]==R&&N[b][a-1]==false) block(b,a-1); //???
		if(map[b-1][a]==R&&N[b-1][a]==false) block(b-1,a); //???
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

bool isMaze;
int blocks[105][105], xg, yg, w,h;

void dfs(int target, int x, int y){
    if(x < 0 || y < 0 || x >= w || y >= h) return;
    if(x == xg && y == yg){
        isMaze = true;
        return;
    }
    if(blocks[y][x] != target || blocks[y][x] == 0){
        return;
    }

    blocks[y][x] = 0;
    
    dfs(target, x + 1, y);
    dfs(target, x - 1, y);
    dfs(target, x, y + 1);
    dfs(target, x, y - 1);
}

int main(){
    int xs, ys, n;
    while(cin >> w >> h, w||h){
        rep(i,105) rep(j,105) blocks[i][j] = 0;
        cin >> xs >> ys >> xg >> yg >> n;
        xs--; ys--; xg--; yg--;
        rep(i,n){
            int c, d, x, y;
            cin >> c >> d >> x >> y;
            x--; y--;
            if(d == 0){
                rep(j,2) rep(k,4) blocks[y + j][x + k] = c;
            }else{
                rep(j,4) rep(k,2) blocks[y + j][x + k] = c;
            }
        }
        isMaze = false;
        dfs(blocks[ys][xs], xs, ys);

        if(isMaze) cout << "OK" << endl;
        else cout << "NG" << endl;
    }
}

C++: //23
#include<iostream>

using namespace std;

int w,h;

void ff(int (*g)[101],int c,int y,int x){
  if(1<=y&&y<=h&&1<=x&&x<=w&&g[y][x]==c){
    g[y][x]=-1;
    int d[]={0,1,0,-1,0};
    for(int i=0;i<4;i++){
      ff(g,c,y+d[i],x+d[i+1]);
    }
  }
}

int main(){
  for(;cin>>w>>h,w|h;){
    int g[101][101]={{}};
    int sx,sy,gx,gy;
    cin>>sx>>sy>>gx>>gy;
    int n;
    cin>>n;
    while(n--){
      int c,d,x,y;
      cin>>c>>d>>x>>y;
      for(int i=0;i<2+2*d;i++){
	for(int j=0;j<4-2*d;j++){
	  g[i+y][j+x]=c;
	}
      }
    }
    ff(g,g[sy][sx],sy,sx);
    cout<<((g[gy][gx]==-1)?"OK":"NG")<<endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
int field[128][128];
bool memo[128][128];
int dx[] = {-1,0,0,1},dy[] = {0,-1,1,0};
int w,h;
void func(int x,int y,int c){
	if(x < 0 || y < 0 || x >= w || y >= h)return;
	if(memo[y][x])return ;
	if(field[y][x] != c)return ;
	memo[y][x] = 1;
	for(int i = 0; i < 4; i++){
		func(x+dx[i],y+dy[i],c);
	}
}
int main(){
	while(scanf("%d%d",&w,&h),w,h){
		int xs,ys,xg,yg,n;
		memset(field,0,sizeof(field));
		memset(memo,0,sizeof(memo));
		scanf("%d%d%d%d%d",&xs,&ys,&xg,&yg,&n);
		for(int i = 0; i < n; i++){
			int c,d,x,y;
			scanf("%d%d%d%d",&c,&d,&x,&y);
			if(d == 0){
				for(int j = x; j < x + 4; j++){
					field[y][j] = c;
					field[y+1][j] = c;
				}
			}
			else{
				for(int j = y; j < y + 4; j++){
					field[j][x] = c;
					field[j][x+1] = c;
				}
			}
		}
		func(xs,ys,field[ys][xs]);
		cout << (memo[yg][xg]==1?"OK":"NG") << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep2(i, n) for(int i = 1; i <= n; ++i)
#define INF 0
#define MAX 102

using namespace std;

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
 
int field[102][102];
int h, w, sx, sy, gx, gy, n;
int col, dire, x, y;
int scol;

bool dfs(int nx, int ny) {
	if(nx == gx && ny == gy) return true;
	field[nx][ny] = -1;
	for(int i = 0; i < 4; ++i) {
		int nex = nx + dx[i];
		int ney = ny + dy[i];
		if(0 < nex && nex <= w && 0 < ney && ney <= h && scol == field[nex][ney]) {
			if(dfs(nex, ney)) return true;
		}
	}
	return false;
}

int main() {
	while(1) {
		scol = INF;
		cin >> h >> w;
		for(int i = 1; i <= h; ++i) {
			for(int j = 1; j <= w; ++j) {
				field[i][j] = 0;
			}
		}
		if(h == 0 && w == 0) break;
		cin >> sx >> sy >> gx >> gy;
		cin >> n;
		rep(k, n) {
			cin >> col >> dire >> x >> y;
			if(dire == 1) {
				for(int i = 0; i < 4; ++i) {
					for(int j = 0; j < 2; ++j) field[x+j][y+i] = col;
				}a
			}
			else {
				for(int i = 0; i < 2; ++i) {
					for(int j = 0; j < 4; ++j) field[x+j][y+i] = col;
				}
			}
		}
		scol = field[sx][sy];
		if(dfs(sx, sy)) cout << "OK" << endl;
		else cout << "NG" << endl; 
	}
	return 0;
}