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>
using namespace std;

int C[102][102];

void meiro(int W,int H,int c){
  if(C[W][H]!=c){
    return;
  }

  C[W][H] = -1;

  meiro(W-1,H  ,c);
  meiro(W  ,H+1,c);
  meiro(W+1,H  ,c);
  meiro(W  ,H-1,c);
}

int main(){
  while(1){
    int w,h;
    cin >> w >> h;
    if(w==0 && h==0){
      break;
    }

    for(int i=0;i<102;i++){
      for(int j=0;j<102;j++){
        C[i][j] = 0;
      }
    }

    int xs,ys,xg,yg;
    cin >> xs >> ys >> xg >> yg;

    int n;
    cin >> n;

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

      if(d==0){
        for(int j=x;j<x+4;j++){
          for(int k=y;k<y+2;k++){
            C[j][k]=c;
          }
        }
      }else{
        for(int j=x;j<x+2;j++){
          for(int k=y;k<y+4;k++){
            C[j][k]=c;
          }
        }
      }
    }
    if(C[xs][ys]==0){
      cout << "NG" << endl;
      continue;
    }

    meiro(xs,ys,C[xs][ys]);

    if(C[xg][yg] == -1){
      cout << "OK" << endl;
    }else{
      cout << "NG" << endl;
    }
  }

  return 0;
}

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] || field[s.x][s.y] != field[g.x][g.y])
		{
			cout << "NG" << endl;
			continue;
		}

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

			int ddx[] = {1, 1, 0, 0}, ddy[] = {0, 0, 1, 1};
			rep(i, 4)
			{
				int nx = p.x + ddx[i], ny = p.y + ddy[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>
using namespace std;

int f[102][102];

bool dfs(int ys,int xs,int yg,int xg,int c){
  if(f[ys][xs]!=c)return 0;

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

  f[ys][xs]=0;

  bool flag=0;
  flag += dfs(ys+1,xs,yg,xg,c);
  flag += dfs(ys-1,xs,yg,xg,c);
  flag += dfs(ys,xs+1,yg,xg,c);
  flag += dfs(ys,xs-1,yg,xg,c);
  return flag;
}

void set(int c,bool d,int x,int y){
  if(d==0){
    for(int i=0; i<4; i++){
      f[y][x+i]=c;
      f[y+1][x+i]=c;
    }
  }else if(d==1){
    for(int i=0; i<4; i++){
      f[y+i][x]=c;
      f[y+i][x+1]=c;
    }
  }
}
;
int main(){
  while(1){

  int w,h;
  cin >> w >> h;
  if(w==0 && h==0)break;

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

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

  if(dfs(ys,xs,yg,xg,f[ys][xs])){
    cout << "OK\n";
  }else{
    cout << "NG\n";
  }
  }

  return 0;
}

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

int w,h,sx,sy,gx,gy;
int mas[101][101],used[101][101];
int dx[] = {0,1,0,-1},dy[] = {1,0,-1,0};

void make_map(int x,int y,int chk,int color){
  if(chk == 0){
    for(int i=y;i<y+2;i++){
      for(int j=x;j<x+4;j++) mas[i][j] = color;
    }
  }
  else {
    for(int i=y;i<y+4;i++){
      for(int j=x;j<x+2;j++) mas[i][j] = color;
    }
  }
}

bool dfs(int x,int y){
  used[y][x] = true;
  if(x == gx && y == gy) return true;
  for(int i=0;i<4;i++){
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(nx >= 0 && nx < w && ny >= 0 && ny < h && mas[ny][nx] == mas[sy][sx] && !used[ny][nx]) dfs(nx,ny);
  }
  return used[gy][gx];
}

int main(){
  int n;
  while(cin >> w >> h,w||h){
    fill_n(mas[0],101*101,0);
    memset(used,false,sizeof(used));
    cin >> sx >> sy >> gx >> gy >> n;
    --sx; --sy; --gx; --gy;
    while(n--){
      int c,d,x,y; cin >> c >> d >> x >> y; --x; --y;
      make_map(x,y,d,c);
    }
    cout << (dfs(sx,sy)? "OK" : "NG") << endl;
  }
}