import java.util.Arrays;

import ru.ifmo.testlib.*;
import ru.ifmo.testlib.Outcome.Type;

public class Check implements Checker {

	public boolean[][] shift(boolean[][] a) {
		int maxx = 0;
		int minx = Integer.MAX_VALUE;
		int maxy = 0;
		int miny = Integer.MAX_VALUE;
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				if (a[i][j]) {
					maxx = Math.max(maxx, i);
					minx = Math.min(minx, i);
					maxy = Math.max(maxy, j);
					miny = Math.min(miny, j);
				}
			}
		}
		if (maxx < minx || maxy < miny) {
			return new boolean[1][1];
		}
		boolean[][] b = new boolean[maxx - minx + 1][maxy - miny + 1];
		for (int i = 0; i < b.length; i++) {
			b[i] = Arrays.copyOfRange(a[i + minx], miny, maxy + 1);
		}
		return b;
	}

	public Outcome test(InStream inf, InStream ouf, InStream ans) {
		int H = ouf.nextInt();
		int W = ouf.nextInt();
		char[][] puzzle = new char[W][H];

		ouf.nextLine();

		for (int i = 0; i < W; i++) {
			puzzle[i] = ouf.nextLine().toCharArray();
			if (puzzle[i].length != H) {
				return new Outcome(Type.PE, "In each line there should be " + H
						+ " characters.");
			}
			// for (int j = 0; j < puzzle[i].length; j++) {
			// if (puzzle[i][j] == '.') {
			// return new Outcome(Type.WA, "The puzzle is incomplete.");
			// }
			// }
		}

		int k = inf.nextInt();
		int h = inf.nextInt();
		int w = inf.nextInt();

		boolean[][][] pieces = new boolean[26][3 * w - 2][3 * h - 2];

		int S = 0;

		for (int i = 0; i < k; i++) {
			for (int x = 0; x < 3 * w - 2; x++) {
				char[] c = inf.nextToken().toCharArray();
				for (int y = 0; y < 3 * h - 2; y++) {
					if (c[y] != '.') {
						pieces[i][x][y] = true;
						S++;
					}
				}
			}

			boolean[][] piece = new boolean[W][H];
			for (int x = 0; x < W; x++) {
				for (int y = 0; y < H; y++) {
					if (puzzle[x][y] == (char) (i + 'A')) {
						piece[x][y] = true;
					}
				}
			}

			pieces[i] = shift(pieces[i]);
			piece = shift(piece);
			if (!Arrays.deepEquals(pieces[i], piece)) {
				return new Outcome(Type.WA, "The piece of char '"
						+ (char) (i + 'A') + "' is wrong.");
			}
		}

		if (S != W * H) {
			return new Outcome(Type.WA, String.format(
					"Area not match: pieces area %d not equal to %d", S, W * H));
		}

		return new Outcome(Type.OK, "The puzzle is completed.");
	}
}
