import java.util.*; import java.util.Map.Entry; import java.math.*; import java.awt.geom.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int h = sc.nextInt(); int w = sc.nextInt(); char[][] map = new char[h][w]; for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { map[i][j] = sc.next().charAt(0); } } int[][] color = new int[h][w]; int id = 0; for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { color[i][j] = id++; } } for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { //if(color[i][j] != id) { DFS(i,j,map,color,i,j,new boolean[h][w]); //} } } int q = sc.nextInt(); for(int i = 0; i < q; i++) { int r = sc.nextInt()-1; int c = sc.nextInt()-1; char x = sc.next().charAt(0); int target = color[r][c]; for(int j = 0; j < h; j++) { for(int k = 0; k < w; k++) { if(color[j][k] == target) map[j][k] = x; } } DFS(r,c,map,color,r,c,new boolean[h][w]); } for(int i = 0; i < h; i++) { System.out.print(map[i][0]); for(int j = 1; j < w; j++) { System.out.print(" " + map[i][j]); } System.out.println(); } } static int[] vx = {0,1,0,-1}; static int[] vy = {1,0,-1,0}; static void DFS(int a, int b, char[][] MAP, int[][] COLOR, int oa, int ob, boolean[][] al) { ArrayDeque queue = new ArrayDeque(); queue.addLast(new Data(a,b,MAP,COLOR,oa,ob,al)); while(!queue.isEmpty()) { Data tmp = queue.pollFirst(); int Ka = tmp.a; int Kb = tmp.b; char[][] KMAP = tmp.MAP; int[][] KCOLOR = tmp.COLOR; int Koa = tmp.oa; int Kob = tmp.ob; boolean[][] Kal = tmp.al; if(Ka < 0 || Kb < 0 || Ka >= KMAP.length || Kb >= KMAP[Ka].length || KMAP[Ka][Kb] != KMAP[Koa][Kob] || Kal[Ka][Kb]) continue; KCOLOR[Ka][Kb] = KCOLOR[Koa][Kob]; Kal[Ka][Kb] = true; for(int i = 0; i < 4; i++) { int ty = vy[i] + Ka; int tx = vx[i] + Kb; queue.addLast(new Data(ty,tx,KMAP,KCOLOR,Koa,Kob,Kal)); } } } static class Data { int a; int b; char[][] MAP; int[][] COLOR; int oa; int ob; boolean[][] al; Data(int a, int b, char[][] MAP, int[][] COLOR, int oa, int ob, boolean[][] al) { this.a = a; this.b = b; this.MAP = MAP; this.COLOR = COLOR; this.oa = oa; this.ob = ob; this.al = al; } } }