void union(int i, int j, int id, int H) { for (int k = 0; k < 4; k++) { int ni = i + move[k][0]; int nj = j + move[k][1]; if (0 <= ni && ni < h && 0 <= nj && nj < w && !u[ni][nj] && map[ni][nj] == H) { Tank t = ref.get(id); t.add(ni, nj); ref.put(id, t); u[ni][nj] = true; tanks[ni][nj] = id; union(ni, nj, id, H); } } }
void run() { Scanner sc = new Scanner(System.in); for (; ; ) { w = sc.nextInt(); h = sc.nextInt(); if ((w | h) == 0) break; fj = sc.nextInt(); fi = sc.nextInt(); q = sc.nextInt(); map = new int[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) map[i][j] = sc.nextInt(); ref = new HashMap<Integer, Tank>(); tanks = new int[h][w]; u = new boolean[h][w]; int id = 1; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { if (map[i][j] == 0 || u[i][j]) continue; Tank t = new Tank(id); t.add(i, j); tanks[i][j] = id; ref.put(id, t); u[i][j] = true; union(i, j, id, map[i][j]); id++; } int L = sc.nextInt(); while (L-- != 0) { int time = sc.nextInt(); int pj = sc.nextInt(), pi = sc.nextInt(); for (int i : ref.keySet()) ref.get(i).height = 0; flow(time); System.out.println((int) ref.get(tanks[pi][pj]).height); } } }