import java.util.*; public class Main { private static Scanner sc = new Scanner(System.in); private static int[][] ary; private static int h; private static int w; public static void main(String[] args) throws Exception { h = sc.nextInt(); w = sc.nextInt(); ary = new int[h][w]; for (int i = 0;i < h;i++) { for (int j = 0;j < w;j++) { ary[i][j] = sc.nextInt(); } } int q = sc.nextInt(); for (int i = 0;i < q;i++) { int x = sc.nextInt() - 1; int y = sc.nextInt() - 1; int c = sc.nextInt(); int pc = ary[x][y]; draw(x, y, c, pc); } show(); } private static void draw(int x, int y, int c, int pc) { ary[x][y] = c; int[] dx = {-1, 0, 1, 0}; int[] dy = {0, 1, 0, -1}; for (int i = 0;i < 4;i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w) { int nc = ary[nx][ny]; if (pc == nc && c != nc) { draw(nx, ny, c, pc); } } } } private static void show() { for (int i = 0;i < h;i++) { StringBuilder sb = new StringBuilder(); for (int j = 0;j < w;j++) { sb.append(ary[i][j]).append(" "); } System.out.println(sb.substring(0, sb.length()-1)); } } }