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<cstdio>
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
int w,h,xs,ys,xg,yg,n,c,d,x,y,nok,sc;
int fld[101][101];
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
void dfs(int fx,int fy){
    if(fld[fy][fx]!=sc||nok==1||fx<0||fx>w-1||fy<0||fy>h-1)return;
    if(fx==xg-1&&fy==yg-1){
        if(sc==fld[fy][fx])nok=1;
    }
    fld[fy][fx]=0;
    for(int i=0;i<4;i++){
        dfs(fx+dx[i],fy+dy[i]);
    }
}
signed main(){
    for(;;){
        cin>>w>>h;
        if(w==0&&h==0)break;
        cin>>xs>>ys;
        cin>>xg>>yg;
        cin>>n;
        nok=0;
        fill(fld[0],fld[0]+101,0);
        for(int i=0;i<n;i++){
            cin>>c>>d>>x>>y;
            x--,y--;
            if(d==0){
                for(int j=0;j<2;j++){
                    for(int k=0;k<4;k++){
                        fld[y+j][x+k]=c;
                    }
                }
            }
            if(d==1){
                for(int j=0;j<4;j++){
                    for(int k=0;k<2;k++){
                        fld[y+j][x+k]=c;
                    }
                }
            }
        }
        sc=fld[ys][xs];
        dfs(0,0);
        if(nok==1)cout<<"OK"<<endl;
        else cout<<"NG"<<endl;
    }
    return 0;
}

C++: #include <iostream>
using namespace std;

int map[102][102];

void route(int w, int h, int color) {
	if (map[w][h] != color)
		return;
	map[w][h] = 0;
	route(w, h + 1, color);
	route(w + 1, h, color);
	route(w, h - 1, color);
	route(w - 1, h, color);
}


int main() {
	int h, w, xs, ys, xg, yg, n;
	int c, d, x, y,color;

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

		for (int i = 0; i<n; i++) {
			cin >> c >> d >> x >> y;
			if (d == 0) {
				for (int j = 0; j < 4; j++)
					map[j + x][y] = map[j + x][y+1] = c;
			}
			else {
				for (int j = 0; j < 4; j++)
					map[x][y + j] = map[x + 1][y + j] = c;
			}
		}
		color = map[xs][ys];
		route(xs, ys, color);
		if (map[xg][yg]==0)
			cout << "OK" << endl;
		else
			cout << "NG"<<endl;
	}

	return 0;
}

C++: #include <stdio.h>

void put(int,int,int,int);
void tansaku(int,int);

const int nashi=-1;
const int tootta=10;

int field[102][102];
int main(int argc,char* argv[]){
	while(1){
		int x_size,y_size;
		int sx,sy;
		int gx,gy;
		int n;
		scanf("%d %d",&x_size,&y_size);
		if(x_size==0)
			return 0;

		scanf("%d %d",&sx,&sy);
		scanf("%d %d",&gx,&gy);
		scanf("%d",&n);

		for(int i=0;i<y_size+2;i++)
			for(int j=0;j<x_size+2;j++)
				field[i][j]=nashi;

		for(int i=0;i<n;i++){
			int c,d,x,y;
			scanf("%d %d %d %d",&c,&d,&x,&y);
			put(c,d,x,y);
		}

		tansaku(sx,sy);
		if(field[gy][gx]==tootta)
			printf("OK\n");
		else
			printf("NG\n");
	}
}

void put(int c,int d,int x,int y){
	const int size[2][2]={{4,2},{2,4}};
	for(int i=0;i<size[d][1];i++){
		for(int j=0;j<size[d][0];j++){
			field[y+i+1][x+j+1]=c;
		}
	}
}

void tansaku(int sx,int sy){
	int c=field[sy][sx];
	field[sy][sx]=tootta;
	if(field[sy][sx-1]==c)
		tansaku(sx-1,sy);
	if(field[sy][sx+1]==c)
		tansaku(sx+1,sy);
	if(field[sy-1][sx]==c)
		tansaku(sx,sy-1);
	if(field[sy+1][sx]==c)
		tansaku(sx,sy+1);
}

C++: #include <iostream>
#include <vector>
using namespace std;

int needleX[4] = {-1, 0, 1, 0};
int needleY[4] = {0, 1, 0, -1};

bool dfs(int x, int y, int goalx, int goaly,vector<vector<int> >& canGo){

    //??´??????????????????????????????true
    if(x == goalx && y == goaly){ return true; }

    //???????????°????????¢?´¢???????????????
    canGo[x][y] = false;

    //???????????????????????¢?´¢
    for(int i = 0; i < 4; i++){
        int nextx = x + needleX[i];
        int nexty = y + needleY[i];

        //?????????????????????
        if(canGo[nextx][nexty]){
            bool flag = dfs(nextx, nexty, goalx, goaly, canGo);
            if(flag){
                return true;
            }
        }
    }

    return false;
}


int block(int w, int h){

    //0 0??§??????
    if(w == 0 && h == 0){return 0;}

    //??????????????±??\???
    int xs, ys, xg, yg;
    int n;
    int c, d, inputX, inputY;
    vector<vector<int> > canGo(h + 2, vector<int>(w + 2, 0));
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;

    //??????????????????
    for(int i = 1; i <= n; i++){
        cin >> c >> d >> inputX >> inputY;
        if(d == 0){
            for(int j = 0; j < 4; j++){
                for(int k = 0; k < 2; k++){
                    canGo[inputY + k][inputX + j] = c;
                }
            }
        }else{
            for(int j = 0; j < 2; j++){
                for(int k = 0; k < 4; k++){
                    canGo[inputY + k][inputX + j] = c;
                }
            }
        }   
    }
    /*??????????????¨???
    cout << canGo[ys][xs] << endl;
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            cout << canGo[i][j];
        }
        cout << endl;
    }
    cout << endl;
    */

    //????????????bool????????¨?????????
    int load = canGo[ys][xs];
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            if(canGo[i][j] == load){
                canGo[i][j] = 1;
            }else{
                canGo[i][j] = 0;
            }
        }
    }
    
    // ??±???????????¢?´¢????????????????????????
    bool result = dfs(xs,ys,xg,yg,canGo);
    if(result){
        return 1;
    }else{
        return 2;
    }

    
}

int main(void){
    int i = 0;
    int array[30];
    int w, h;
    while(1){
        cin >> w >> h;
        array[i] = block(w, h);
        if(array[i] == 0){break;}
        i++;
    }
    for(int j = 0; j < i; j++){
        if(array[j] == 1){cout << "OK" << endl;}
        else {cout << "NG" << endl;}
    }

    return 0;
}