// Problem checker for Bloxx problem
// Author : Vladimir Ulyantsev, ulyantsev@rain.ifmo.ru
uses 
	testlib, sysutils;
type
	int = longint;
const
	MAXN = 10;

var
	n, m, i, j, x, y, h : int;
	a : array [0..MAXN + 1, 0..MAXN + 1] of int;

	ph, juryHeight, participantHeight, participantSteps : int;

function isValid(x, y : int) : boolean;
var
	k : int;
	numbers : array [0..5] of boolean;
begin
	result := true;	
	fillchar(numbers, sizeof(numbers), 0);
	numbers[a[x - 1][y]] := true;
	numbers[a[x][y - 1]] := true;
	numbers[a[x + 1][y]] := true;
	numbers[a[x][y + 1]] := true;

	
	for k := 1 to a[x][y] - 1 do begin
		if (not numbers[k]) then begin
			result := false;
		end;
	end;
end;

begin
	n := inf.ReadLongint;
	m := inf.ReadLongint;
	ph := 0;
	for i := 1 to n do begin
		for j := 1 to m do begin
			a[i][j] := inf.ReadLongint;
			ph := ph + a[i][j];
		end;
	end;

	juryHeight := ans.ReadLongint;
	participantHeight := ouf.ReadLongint;

	if (participantHeight < juryHeight) then
		quit(_wa, 'jury height = ' + IntToStr(juryHeight) + 
				  '; participant height = ' + IntToStr(participantHeight));

	participantSteps := ouf.ReadLongint;

		
	for i := 1 to participantSteps do begin
		x := ouf.ReadLongint;
		y := ouf.ReadLongint;
		if ((x < 1) or (x > n) or (y < 1) or (y > m) or (a[x][y] <> 0)) then
			quit(_wa, 'Participant can''t build a house in (' +
					  IntToStr(x) + ', ' + IntToStr(y) + ')');

		h := ouf.ReadLongint;
		a[x][y] := h;
		ph := ph + h;
		if ((h < 1) or (not isValid(x, y))) then
			quit(_wa, 'Participant can''t build a house height ' + IntToStr(h) + 
				  	  ' in (' + IntToStr(x) + ', ' + IntToStr(y) + ')');
	end;

	if (ph <> participantHeight) then
		quit(_wa, 'Participant is lying about his height');

	if (participantHeight > juryHeight) then
		quit(_fail, 'Participant is better: ' + IntToStr(juryHeight) +
					' < '+ IntToStr(participantHeight));
	
	quit(_ok, 'OK');
end.
