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
#include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define pb push_back
#define OUT(x) {cout << x << "\n"; exit(0); }
#define TCOUT(x) {cout << x << "\n"; return; }
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define sz(x) int(x.size())
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
#ifdef DEBUG
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == prev(x.end()) ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#endif
#ifdef DEBUG
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
typedef vector<int> perm;
typedef vector<vi> intboard;
string empty_line;

int n, m;
intboard empty() {
	return vector<vi>(n, vi(m, -1));
}

intboard move_G(const intboard &b) {
	debug("moving G / 0");
	intboard ret = empty();
	rep(j, 0, m) {
		int first_free = 0;
		rep(i, 0, n) {
			if (b[i][j] != -1) {
				ret[first_free][j] = b[i][j];
				first_free++;
			}
		}
	}
	return ret;
}

intboard move_D(const intboard &b) {
	debug("moving D / 2");
	intboard ret = empty();
	rep(j, 0, m) {
		int first_free = n - 1;
		for (int i = n - 1; i >= 0; --i) {
			if (b[i][j] != -1) {
				ret[first_free][j] = b[i][j];
				first_free--;
			}
		}
	}
	return ret;
}

intboard move_L(const intboard &b) {
	debug("moving L / 1");
	intboard ret = empty();
	rep(i, 0, n) {
		int first_free = 0;
		rep(j, 0, m) {
			if (b[i][j] != -1) {
				ret[i][first_free] = b[i][j];
				first_free++;
			}
		}
	}
	return ret;
}

intboard move_P(const intboard &b) {
	debug("moving P / 3");
	intboard ret = empty();
	rep(i, 0, n) {
		int first_free = m - 1;
		for (int j = m - 1; j >= 0; --j) {
			if (b[i][j] != -1) {
				ret[i][first_free] = b[i][j];
				first_free--;
			}
		}
	}
	return ret;
}

inline int type(char zn) {
	if (zn == 'G') return 0;
	if (zn == 'L') return 1;
	if (zn == 'D') return 2;
	return 3;
}

intboard move(const intboard &b, int t) {
	if (t == 0) return move_G(b);
	if (t == 1) return move_L(b);
	if (t == 2) return move_D(b);
	return move_P(b);
}

bool TEST = 0;
int random(int a, int b) { return a + rand()%(b-a+1); }
inline bool opposite(int a, int b) {
	return abs(a - b) == 2;
}
int circ(int a, int b) {
	return (a - b + 4) % 4;
}

bool parsing_correct(vi moves) {
	if (sz(moves) <3 ) return true;
	int delta = circ(moves[1], moves[0]);
	for (int i=1; i<sz(moves); ++i) {
		if (circ(moves[i], moves[i - 1]) != delta) return false;
	}
	
	return true;
}

int nr(int i, int j) {
	return i * m + j;
}

perm zloz(const perm &a, const perm &b) { // first a, then b
	int N = sz(a);
	assert(sz(b) == N);
	perm result(N);
	rep(i, 0, N) result[i] = b[a[i]];
	return result;
}

perm poteguj(perm p, int x) { // podnies permutacje do potegi x
	x -= 1;
	perm result = p;
	while (x > 0) {
		if (x & 1) result = zloz(result, p);
		p = zloz(p, p);
		x /= 2;
	}
	return result;
}

void print(const intboard &b) {
	rep(i, 0, n) {
		rep(j, 0, m) cout << b[i][j] << ' ';
		cout << "\n";
	}
	cout << endl;
}

int main() {
	cin >> n >> m;
	vector<string> input(n);
	if (!TEST) {
		rep(i, 0, n) cin >> input[i];
	}
	vector<char> symbols;
	intboard tab = empty();
	rep(i, 0, n) {
		rep(j, 0, m) {
			if (input[i][j] != '.') {
				tab[i][j] = sz(symbols);
				symbols.push_back(input[i][j]);
			}
		}
	}

	int q; 
	cin >> q;
	string s;
	cin >> s;
	vi moves(q);
	rep(i, 0, q) moves[i] = type(s[i]);
	vi stos = {moves[0]};
	rep(i, 1, q) {
		int SZ = sz(stos);
		int znak = moves[i];
		if (znak == stos[SZ - 1]) continue;
		if (SZ > 1 && znak == stos[SZ - 2]) continue; //two last characters checked
		if (opposite(znak, stos[SZ - 1])) { //they cancel each other out
			stos.pop_back();
			if (sz(stos) < 2) stos.push_back(znak);
			continue;
		}
		
		stos.pb(znak);
	}
	
	assert(parsing_correct(stos));
	reverse(all(stos));
	rep(step, 0, 4) {
		if (!stos.empty()) {
			tab = move(tab, stos.back());
			stos.pop_back();
		}
	}
	
	// initial moves done
	intboard full_board = tab;
	int full_cycles = sz(stos) / 4;
	if (full_cycles > 0) {
		for (int i = 1; i <= 4; ++i) {
			int ruch = stos[sz(stos) - i];
			full_board = move(full_board, ruch);
		}
		rep(i, 0, n) {
			rep(j, 0, m) {
				bool b1 = (tab[i][j] == -1);
				bool b2 = (full_board[i][j] == -1);
				assert(b1 == b2);
			}
		}
		
		
		
		vi gdzie(sz(symbols), -1);
		rep(i, 0, n) {
			rep(j, 0, m) {
				if (full_board[i][j] == -1) continue;
				gdzie[full_board[i][j]] = nr(i, j);
			}
		}
		
		perm p(n * m);
		rep(i, 0, n) {
			rep(j, 0, m) {
				int numer = nr(i, j);
				if (tab[i][j] == -1) p[numer] = numer;
				else {
					assert(gdzie[tab[i][j]] != -1);
					p[numer] = gdzie[tab[i][j]];
				}
			}
		}
		
		perm wynikowa = poteguj(p, full_cycles);
		intboard po_cyklach = tab;
		rep(i, 0, n) {
			rep(j, 0, m) {
				int numer = nr(i, j);
				int gdzie_pojdzie = wynikowa[numer];
				po_cyklach[gdzie_pojdzie / m][gdzie_pojdzie % m] = tab[i][j];
			}
		}
		
		tab = po_cyklach;
	}
	
	
	// koncowka 
	rep(step, 0, 4 * full_cycles) stos.pop_back();
	while (!stos.empty()) {
		tab = move(tab, stos.back());
		stos.pop_back();
	}
	
	
	// reverting enumeration
	rep(i, 0, n) {
		rep(j, 0, m) {
			if (tab[i][j] == -1) cout << '.';
			else cout << symbols[tab[i][j]];
		}
		cout << "\n";
	}
	
	
}