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
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
constexpr int N = 510, K = 5e5 + 10;
int n, m, k, a[N][N], b[N][N], c[N][N];
int cnt;
pair<int, int> rev[N * N];
string str;
struct Perm {
	int p[N * N];
	Perm() {iota(p + 1, p + cnt + 1, 1);}
	int& operator [] (int x) {return p[x];}
	friend Perm operator * (Perm x, Perm y) {
		Perm z;
		for(int i = 1; i <= cnt; i ++) z[i] = y[x[i]];
		return z;
	}
};
void up(int (*a)[N]) {
	for(int j = 1; j <= m; j ++) {
		int p = 1;
		for(int i = 1; i <= n; i ++) if(a[i][j]) {
			a[p][j] = a[i][j], p ++;
		}
		for(int i = p; i <= n; i ++) a[i][j] = 0;
	}
}
void down(int (*a)[N]) {
	for(int j = 1; j <= m; j ++) {
		int p = n;
		for(int i = n; i >= 1; i --) if(a[i][j]) {
			a[p][j] = a[i][j], p --;
		}
		for(int i = p; i >= 1; i --) a[i][j] = 0;
	}
}
void left(int (*a)[N]) {
	for(int i = 1; i <= n; i ++) {
		int p = 1;
		for(int j = 1; j <= m; j ++) if(a[i][j]) {
			a[i][p] = a[i][j], p ++;
		}
		for(int j = p; j <= m; j ++) a[i][j] = 0;
	}
}
void right(int (*a)[N]) {
	for(int i = 1; i <= n; i ++) {
		int p = m;
		for(int j = m; j >= 1; j --) if(a[i][j]) {
			a[i][p] = a[i][j], p --;
		}
		for(int j = p; j >= 1; j --) a[i][j] = 0;
	}
}
Perm fpw(Perm a, int b) {
	Perm ans;
	for(; b; b >>= 1, a = a * a) if(b & 1) ans = ans * a;
	return ans;
}
int main() {
	// freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n >> m; 
	for(int i = 1; i <= n; i ++) {
		string str;
		cin >> str;
		str = "?" + str;
		for(int j = 1; j <= m; j ++) a[i][j] = str[j] != '.' ? str[j] - 'A' : 0;
	}
	cin >> k >> str;
	string oper;
	for(int i = 0; i < k; i ++) {
		if(str[i] == 'L' || str[i] == 'P') {
			if(oper.size() && (oper.back() == 'L' || oper.back() == 'P')) {
				oper.pop_back();
			} 
			oper += str[i];
			if(oper.size() > 2 && oper[oper.size() - 3] == str[i]) oper.pop_back();
		} else {
			if(oper.size() && (oper.back() == 'G' || oper.back() == 'D')) {
				oper.pop_back();
			} 
			oper += str[i];
			if(oper.size() > 2 && oper[oper.size() - 3] == str[i]) oper.pop_back();
		}
	}
	for(int i = 0; i + 4 < oper.size(); i ++) {
		assert(oper[i] == oper[i + 4]);
	}
	if(oper.size() < 8) {
		for(; oper.size(); ) {
			if(oper[0] == 'G') up(a);
			else if(oper[0] == 'D') down(a);
			else if(oper[0] == 'L') left(a);
			else if(oper[0] == 'P') right(a);
			oper.erase(oper.begin());
		}
		for(int i = 1; i <= n; i ++) {
			for(int j = 1; j <= m; j ++) {
				cout << char(a[i][j] ? a[i][j] + 'A' : '.');
			}
			cout << '\n';
		}
		return 0;
	}
	for(int i = 0; i < 4 && oper.size(); i ++) {
		if(oper[0] == 'G') up(a);
		else if(oper[0] == 'D') down(a);
		else if(oper[0] == 'L') left(a);
		else if(oper[0] == 'P') right(a);
		oper.erase(oper.begin());
	}
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= m; j ++) if(a[i][j]) {
			b[i][j] = c[i][j] = ++ cnt;
			rev[cnt] = {i, j};
		}
	}
	for(int i = 0; i < 4; i ++) {
		if(oper[i] == 'G') up(c);
		else if(oper[i] == 'D') down(c);
		else if(oper[i] == 'L') left(c);
		else if(oper[i] == 'P') right(c);
	}
	Perm base;
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= m; j ++) if(a[i][j]) {
			assert(b[i][j] && c[i][j]);
			base[c[i][j]] = b[i][j];
		}
	}
	base = fpw(base, oper.size() / 4);
	memset(c, 0, sizeof(c));
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= m; j ++) if(a[i][j]) {
			auto [x, y] = rev[base[b[i][j]]];
			c[x][y] = a[i][j];
		}
	}
	for(int i = oper.size() / 4 * 4; i < oper.size(); i ++) {
		if(oper[i] == 'G') up(c);
		else if(oper[i] == 'D') down(c);
		else if(oper[i] == 'L') left(c);
		else if(oper[i] == 'P') right(c);
	}
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= m; j ++) {
			cout << char(c[i][j] ? c[i][j] + 'A' : '.');
		}
		cout << '\n';
	}
 	return 0;
}