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
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#pragma warning(disable:4786)
#pragma warning(disable:4996)
#include <random>
#include <chrono>
#include <ctime>
#include<list>
#include <numeric>
#include<bitset>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<functional>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<utility>
#include<fstream>
#include<sstream>
#include<cmath>
#include<stack>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
#include <array>
#include <complex>
#include<iomanip>
using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) )
#define S(X) ( (X) * (X) )
#define SZ(V) (int )V.size()
#define FORN(i, n) for(int i = 0; i < n; i++)
#define FORAB(i, a, b) for(int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C)  ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))

typedef long long int LL;
//typedef __int128 LLL;
typedef long long LLL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<LL, int> PLI;
typedef pair<double, double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VP;
typedef vector<double> VD;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef long double ld;
typedef unsigned long long ULL;

//#define MAXN 1000
//#define MAXN2 MAXN*MAXN
#define MAXN 100

//const LL MOD[2] = { 87420317, 1000000007 };
const LL MOD = 1000000007;
//const LL MOD = 998244353;
//const LL MOD = 547892069;
//const LL INF = 2000000000000000001LL; //2e18 + 1

int n, m;
char board[502][502];
char cmd[500005];

int IsLR(char ch) {
	return ch == 'L' || ch == 'P';
}

int IsUD(char ch) {
	return 1 - IsLR(ch);
}

int Id(string s) {
	s = s.substr(0, 2);
	if (s == "DL" || s == "LD") return 0;
	if (s == "GL" || s == "LG") return 1;
	if (s == "GP" || s == "PG") return 2;
	if (s == "DP" || s == "PD") return 3;
}

int HowMuchTurn(int id, char ch) {
	if (id == 0) {
		if (ch == 'G') return 1;
		else if (ch == 'P') return -1;
		else return 0;
	}
	if (id == 1) {
		if (ch == 'P') return 1;
		else if (ch == 'D') return -1;
		else return 0;
	}
	if (id == 2) {
		if (ch == 'D') return 1;
		else if (ch == 'L') return -1;
		else return 0;
	}
	if (id == 3) {
		if (ch == 'L') return 1;
		else if (ch == 'G') return -1;
		else return 0;
	}
}

int ApplyTurn(int q, char ch) {
	q += HowMuchTurn(q, ch);
	q = (q + 4) % 4;
	return q;
}

PII pboard[502][502];
PII rev[502][502];
struct Pos {
	int q, r, c;
};
vector<Pos> moves[502][502];

template<typename T>
void ApplyMove(char ch, T B[502][502], T empty) {
	if (ch == 'L') {
		for (int i = 0; i < n; i++) {
			int f = 0;
			for (int j = 0; j < m; j++) {
				if (B[i][j] != empty) {
					B[i][f] = B[i][j];
					if (f != j) B[i][j] = empty;
					f++;
				}
			}
		}
	}
	if (ch == 'P') {
		for (int i = 0; i < n; i++) {
			int f = m - 1;
			for (int j = m - 1; j >= 0; j--) {
				if (B[i][j] != empty) {
					B[i][f] = B[i][j];
					if (f != j) B[i][j] = empty;
					f--;
				}
			}
		}
	}
	if (ch == 'G') {
		for (int j = 0; j < m; j++) {
			int f = 0;
			for (int i = 0; i < n; i++) {
				if (B[i][j] != empty) {
					B[f][j] = B[i][j];
					if (f != i) B[i][j] = empty;
					f++;
				}
			}
		}
	}
	if (ch == 'D') {
		for (int j = 0; j < m; j++) {
			int f = n - 1;
			for (int i = n - 1; i >= 0; i--) {
				if (B[i][j] != empty) {
					B[f][j] = B[i][j];
					if (f != i) B[i][j] = empty;
					f--;
				}
			}
		}
	}
}

int mark[502][502];

struct Where {
	PII start;
	int pos;
};
Where where[502][502];

void build(int step, int q) {
	FORN(i, n) FORN(j, m) pboard[i][j] = (board[i][j] == '.' ? PII(-1, -1) : PII(i, j));
	string cmd;
	if (q == 0) cmd = step > 0 ? "GPDL" : "PGLD";
	if (q == 1) cmd = step > 0 ? "PDLG" : "DPGL";
	if (q == 2) cmd = step > 0 ? "DLGP" : "LDPG";
	if (q == 3) cmd = step > 0 ? "LGPD" : "GLDP";

	for (int x = 0; x < 4; x++) {
		FORN(i, n) FORN(j, m) {
			if (pboard[i][j] != PII(-1, -1)) {
				PII Z = pboard[i][j];
				moves[Z.first][Z.second].push_back({ q, i, j });
			}
		}
		q = ApplyTurn(q, cmd[x]);
		ApplyMove(cmd[x], pboard, PII(-1, -1));
	}

	FORN(i, n) FORN(j, m) if (pboard[i][j] != PII(-1, -1)) {
		rev[pboard[i][j].first][pboard[i][j].second] = { i, j };
	}
	FORN(i, n) FORN(j, m) {
		if (mark[i][j]) continue;
		if (pboard[i][j] == PII(-1, -1)) continue;
		PII cur = rev[i][j];
		mark[i][j] = 1;
		where[i][j] = { {i, j}, 0 };
		while (mark[cur.first][cur.second] == 0) {
			where[cur.first][cur.second] = { {i, j}, SZ(moves[i][j]) };
			for (int k = 0; k < 4; k++) {
				moves[i][j].push_back(moves[cur.first][cur.second][k]);
			}
			mark[cur.first][cur.second] = 1;
			cur = rev[cur.first][cur.second];
		}
	}
}

char finale[502][502];

Pos Find(PII s, int turn) {
	Where wh = where[s.first][s.second];
	wh.pos = (wh.pos + turn) % moves[wh.start.first][wh.start.second].size();
	return moves[wh.start.first][wh.start.second][wh.pos];
}

void solve(int ks) {
	scanf("%d %d", &n, &m);
	FORN(i, n) scanf("%s", board[i]);
	int len; scanf("%d", &len);
	scanf("%s", cmd);
	string S;
	for (int i = 0; cmd[i]; i++) {
		if (S.empty()) {
			S.push_back(cmd[i]);
			continue;
		}

		if (IsLR(cmd[i]) && IsLR(S.back())) {
			S.back() = cmd[i];
		}
		else if (IsUD(cmd[i]) && IsUD(S.back())) {
			S.back() = cmd[i];
		}
		else {
			S.push_back(cmd[i]);
		}
	}

	for (int i = 0; i < S.size() && i < 2; i++) {
		ApplyMove(S[i], board, '.');
	}

	if (S.size() <= 2) {
		for (int i = 0; i < n; i++) printf("%s\n", board[i]);
		return;
	}

	int id = Id(S);
	int turn = 0;
	int running = id;
	for (int i = 2; i < S.size(); i++) {
		turn += HowMuchTurn(running, S[i]);
		running = ApplyTurn(running, S[i]);
	}

	if (turn == 0) {
		for (int i = 0; i < n; i++) printf("%s\n", board[i]);
		return;
	}

	int step = (turn > 0 ? 1 : -1);
	turn = ABS(turn);

	build(step, id);

	FORN(i, n) FORN(j, m) finale[i][j] = '.';
	FORN(i, n) FORN(j, m) {
		if (board[i][j] != '.') {
			Pos pos = Find({ i, j }, turn);
			finale[pos.r][pos.c] = board[i][j];
		}
		finale[i][m] = 0;
	}
	FORN(i, n) printf("%s\n", finale[i]);
}

void gen() {
}

int main()
{
	double start_time = clock();
#ifdef LOCAL
	freopen("C:\\Home\\Contests\\F\\sample.in", "r", stdin);
	//freopen("C:\\Home\\Contests\\F\\0.out", "w", stdout);
#endif

	gen();

	if (0) {
		int T;
		scanf("%d", &T);
		//AIN(T, 1, 10);
		for (int ks = 1; ks <= T; ks++) {
			solve(ks);
			//if (ks % 1 == 0) fprintf(stderr, "%d done\n", ks);
		}
	}
	else {
		solve(1);
	}

	double end_time = clock();
	fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC);
	return 0;
}