1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <iostream>
#include <queue>

using namespace std;

const int MAX_N = 509;
const int MAX_K = 500001;

int n,m;

int tab[MAX_N][MAX_N];/// [y] [x]
/// 4 - gora, 1 - prawo, 2 - dol, 3 = lewo, 5 = clockwise, 6 = anticlockwise
int converted_commands[MAX_K/3];
int conv_commands_iterator = 0;

int command_encoder(char command){
	if(command == 'G') return 4;
	else if(command == 'P') return 1;
	else if(command == 'D') return 2;
	else if(command == 'L') return 3;

	throw(error_code());
}

bool is_up_down(int i){
	if(i == 2 or i == 4 or i == 'G' or i == 'D') return true;
	else return false;
}

struct ruchomy_kwadrat{/// clockwise - 3 - gora, 1 - prawo, 2 - dol, 4 - lewo
						/// anticlockwise - 1 - dol, 3 - prawo, 4 - gora, 2 - lewo
	int cur_poz;        ///  1 2
 	int home;           ///  3 4
	bool is_clockwise;
	int first_step_clockwise;
	int last_step_clockwise;
	int last_command;

	void new_command(int command, bool live){

		if(at_home() and live){
			if(command == first_step_clockwise)is_clockwise = true;
			else is_clockwise = false;
		}

		last_command = command;

		if(command == 4){
			if(cur_poz > 2){
				cur_poz-= 2;
				if(live) check_if_home();
			}

			return;
		}
		else if(command == 2){
			if(cur_poz <= 2){
				cur_poz+= 2;
				if(live) check_if_home();
			}

			return;
		}
		else if(command == 1){
			if((cur_poz % 2) == 1){
				cur_poz++;
				if(live) check_if_home();
			}

			return;
		}
		else if(command == 3){
			if((cur_poz % 2) == 0){
				cur_poz--;
				if(live) check_if_home();
			}

			return;
		}
	}

	bool at_home(){
		return cur_poz == home;
	}

	void check_if_home(){
		if(at_home()){
			if(last_command == last_step_clockwise and is_clockwise == true){
				converted_commands[conv_commands_iterator] = 5;
				conv_commands_iterator++;
			}
			else if(last_command != last_step_clockwise and is_clockwise == false){
				converted_commands[conv_commands_iterator] = 6;
				conv_commands_iterator++;
			}
		}
	}

	void finish(){
		int backup_poz = cur_poz;
		if(at_home()) return;
		cur_poz = home;

		int movement;

		while(cur_poz != backup_poz){
			movement = poz_to_movement(cur_poz, is_clockwise);

			converted_commands[conv_commands_iterator] = movement;
			conv_commands_iterator++;

			new_command(movement, false);
		}
	}

	int poz_to_movement(int poz, bool clockwise){/// clockwise - 3 - gora, 1 - prawo, 2 - dol, 4 - lewo
						/// anticlockwise - 1 - dol, 3 - prawo, 4 - gora, 2 - lewo
		if(clockwise){
			if(poz == 4) return 3;
			else if(poz == 1) return 1;
			else if(poz == 2) return 2;
			else if(poz == 3) return 4;
		}
		else{
			if(poz == 4) return 4;
			else if(poz == 1) return 2;
			else if(poz == 2) return 3;
			else if(poz == 3) return 1;
		}
	}

	int movement_to_poz(int movement, int poz){
		int command = movement;
		bool live = false;
		int cur_poz = poz;

		if(command == 4){
			if(cur_poz > 2){
				cur_poz-= 2;
				if(live) check_if_home();
			}

			return cur_poz;
		}
		else if(command == 2){
			if(cur_poz <= 2){
				cur_poz+= 2;
				if(live) check_if_home();
			}

			return cur_poz;
		}
		else if(command == 1){
			if((cur_poz % 2) == 1){
				cur_poz++;
				if(live) check_if_home();
			}

			return cur_poz;
		}
		else if(command == 3){
			if((cur_poz % 2) == 0){
				cur_poz--;
				if(live) check_if_home();
			}

			return cur_poz;
		}
	}



};


void move_tab(int command){
	if(command == 4){
		int countt = 0;
		for(int x = 0; x < m;x++){
			countt = 0;
			for(int y = 0; y < n; y++){
				if(tab[y][x] == 1){
					tab[y][x] = 0;
					tab[countt][x] = 1;
					countt++;
				}
				else if(tab[y][x] == 2){
					tab[y][x] = 0;
					tab[countt][x] = 2;
					countt++;
				}



			}
		}
	}

	else if(command == 2){
		int countt = 0;
		for(int x = 0; x < m;x++){
			countt = 0;
			for(int y = n - 1; y >= 0; y--){
				if(tab[y][x] == 1){
					tab[y][x] = 0;
					tab[n - countt - 1][x] = 1;
					countt++;
				}
				else if(tab[y][x] == 2){
					tab[y][x] = 0;
					tab[n - countt - 1][x] = 2;
					countt++;
				}


			}
		}
	}

	else if(command == 1){
		int countt = 0;
		for(int y = 0; y < n; y++){
			countt = 0;
			for(int x = m - 1; x >= 0;x--){
				if(tab[y][x] == 1){
					tab[y][x] = 0;
					tab[y][m - 1 - countt] = 1;
					countt++;
				}
				else if(tab[y][x] == 2){
					tab[y][x] = 0;
					tab[y][m - 1- countt] = 2;
					countt++;
				}


			}
		}
	}

	else if(command == 3){
		int countt = 0;
		for(int y = 0; y < n; y++){
			countt = 0;
			for(int x = 0; x < m;x++){
				if(tab[y][x] == 1){
					tab[y][x] = 0;
					tab[y][countt] = 1;
					countt++;
				}
				else if(tab[y][x] == 2){
					tab[y][x] = 0;
					tab[y][countt] = 2;
					countt++;
				}


			}
		}
	}
}

void print_tab(){
	string wiersz;
	for(int y = 0; y < n; y++){
		wiersz = "";
		for(int x = 0; x < m; x++){
			if(tab[y][x] == 0)wiersz += '.';
			else if(tab[y][x] == 1) wiersz += 'B';
			else wiersz +='C';
		}
		cout<<wiersz<<"\n";
	}
}

string fast_read_string(){
	char c;
	string w = "";

	c = getchar_unlocked();
	while((c >'Z' or c < 'A') and c != '.')c = getchar_unlocked();
	while(('Z' >=c and c >= 'A') or c == '.'){
		w+= c;
		c = getchar_unlocked();
	}

	return w;

}

int fast_read_int(){
	char c;
	int w = 0;

	c = getchar_unlocked();

	while(c > '9' or c < '0') c = getchar_unlocked();
	while('9'>= c and c >= '0'){
		w = w * 10 + (c - '0');
		c = getchar_unlocked();
	}

	return w;
}


int main()
{
    //int n,m;
   // cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    //cin>>n>>m;
    n = fast_read_int();
    m = fast_read_int();
    string input;

    bool first_move_up = false;

	for(int y = 0; y < n; y++){
		//cin>>input;
		input = fast_read_string();
		for(int x = 0; x < m; x++){
			if(input[x] == 'B'){
				tab[y][x] = 1;
			}
			else if(input[x] == 'C'){
				tab[y][x] = 2;
			}
		}
	}

	int k;
	string polecenia;

    //cin>>k;
    k = fast_read_int();
    //cin>>polecenia;
    polecenia = fast_read_string();

	int starting_up_down;
	int starting_left_right;

	bool init_up_down = false;
	bool init_left_right = false;

	int i = 0;
	int cur_direction;

	while((!init_up_down) or (!init_left_right)){
		cur_direction = command_encoder(polecenia[i]);

		if(is_up_down(cur_direction)){
			init_up_down = true;
			starting_up_down = cur_direction;
			first_move_up = false;
		}
		else{
			init_left_right = true;
			starting_left_right = cur_direction;
			first_move_up = true;
		}


		i++;
		if(i == k){
			if(init_up_down == true and (starting_up_down == 2 or starting_up_down == 4)){
				if(init_left_right == true){
					if(first_move_up){
						move_tab(starting_up_down);
						move_tab(starting_left_right);
					}
					else{
						move_tab(starting_left_right);
						move_tab(starting_up_down);
					}
				}
				else move_tab(starting_up_down);
			}
			else{
				move_tab(starting_left_right);
			}

			print_tab();
			return 0;
		}

	}

	int last_step_clockwise;
	if(starting_up_down == 2){
		if(starting_left_right == 1) last_step_clockwise = 2;
		else last_step_clockwise = 3;
	}
	else{
		if(starting_left_right == 1) last_step_clockwise = 1;
		else last_step_clockwise = 4;
	}

	int first_step_clockwise = (last_step_clockwise + 1)% 5;
	first_step_clockwise += (first_step_clockwise == 0);

	ruchomy_kwadrat kwadrat;

	kwadrat.home = (starting_up_down % 4) + (starting_left_right % 3) + 1;
	kwadrat.cur_poz = kwadrat.home;
	kwadrat.first_step_clockwise = first_step_clockwise;
	kwadrat.last_step_clockwise = last_step_clockwise;

	for(;i < k;i++){
		cur_direction = command_encoder(polecenia[i]);
		kwadrat.new_command(cur_direction, true);

	}

	kwadrat.finish();

	//cout<<"\n";

	//for(int j = 0; j < conv_commands_iterator; j++){
		//cout<<converted_commands[j]<<" ";
	//}

	if(first_move_up){
		move_tab(starting_up_down);
		move_tab(starting_left_right);
	}
	else{
		move_tab(starting_left_right);
		move_tab(starting_up_down);
	}

	for(int j = 0; j < conv_commands_iterator; j++){
		if(converted_commands[j] == 5){
			int poz = kwadrat.home;
			int movement;
			for(int l = 0; l < 4; l++){
				movement = kwadrat.poz_to_movement(poz, true);

				move_tab(movement);
				poz = kwadrat.movement_to_poz(movement, poz);
			}
		}
		else if(converted_commands[j] == 6){
			int poz = kwadrat.home;
			int movement;
			for(int l = 0; l < 4; l++){
				movement = kwadrat.poz_to_movement(poz, false);

				move_tab(movement);
				poz = kwadrat.movement_to_poz(movement, poz);
			}
		}
		else move_tab(converted_commands[j]);
	}

	print_tab();


    return 0;
}