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
#include <iostream>
#include <vector>
#include <stack>
#include <map>
using namespace std;

void move_up(vector<string>&V, int n, int m) {
    for (int j = 0; j < m; j++) {
        int ir = 0, iw = 0;
        for (; ir < n; ir++)
            if (V[ir][j] != '.')
                V[iw++][j] = V[ir][j];
        while (iw < n)
            V[iw++][j] = '.';
    }
}
void move_down(vector<string>&V, int n, int m) {
    for (int j = 0; j < m; j++) {
        int ir = n - 1, iw = n - 1;
        for (; ir >= 0; ir--)
            if (V[ir][j] != '.')
                V[iw--][j] = V[ir][j];
        while (iw >= 0)
            V[iw--][j] = '.';
    }
}
void move_left(vector<string>&V, int n, int m) {
    for (int i = 0; i < n; i++) {
        int jr = 0, jw = 0;
        for (; jr < m; jr++)
            if (V[i][jr] != '.')
                V[i][jw++] = V[i][jr];
        while (jw < m)
            V[i][jw++] = '.';
    }
}
void move_right(vector<string>&V, int n, int m) {
    for (int i = 0; i < n; i++) {
        int jr = m - 1, jw = m - 1;
        for (; jr >= 0; jr--)
            if (V[i][jr] != '.')
                V[i][jw--] = V[i][jr];
        while (jw >= 0)
            V[i][jw--] = '.';
    }
}
void one_move(vector<string>&V, int n, int m, char dir) {
    switch (dir) {
        case 'G':
            move_up(V, n, m);
            break;
        case 'D':
            move_down(V, n, m);
            break;
        case 'L':
            move_left(V, n, m);
            break;
        case 'P':
            move_right(V, n, m);
            break;
    }
}

void move_up_numeric(vector<vector<int>>&V, int n, int m) {
    for (int j = 0; j < m; j++) {
        int ir = 0, iw = 0;
        for (; ir < n; ir++)
            if (V[ir][j] != -1)
                V[iw++][j] = V[ir][j];
        while (iw < n)
            V[iw++][j] = -1;
    }
}
void move_down_numeric(vector<vector<int>>&V, int n, int m) {
    for (int j = 0; j < m; j++) {
        int ir = n - 1, iw = n - 1;
        for (; ir >= 0; ir--)
            if (V[ir][j] != -1)
                V[iw--][j] = V[ir][j];
        while (iw >= 0)
            V[iw--][j] = -1;
    }
}
void move_left_numeric(vector<vector<int>>&V, int n, int m) {
    for (int i = 0; i < n; i++) {
        int jr = 0, jw = 0;
        for (; jr < m; jr++)
            if (V[i][jr] != -1)
                V[i][jw++] = V[i][jr];
        while (jw < m)
            V[i][jw++] = -1;
    }
}
void move_right_numeric(vector<vector<int>>&V, int n, int m) {
    for (int i = 0; i < n; i++) {
        int jr = m - 1, jw = m - 1;
        for (; jr >= 0; jr--)
            if (V[i][jr] != -1)
                V[i][jw--] = V[i][jr];
        while (jw >= 0)
            V[i][jw--] = -1;
    }
}
void one_move_numeric(vector<vector<int>>&V, int n, int m, char dir) {
    /* cerr << "ONE MOVE NUMERIC " << dir << endl; */
    switch (dir) {
        case 'G':
            move_up_numeric(V, n, m);
            break;
        case 'D':
            move_down_numeric(V, n, m);
            break;
        case 'L':
            move_left_numeric(V, n, m);
            break;
        case 'P':
            move_right_numeric(V, n, m);
            break;
    }
}

string op_seq_reduce(const string& ops) {
    int n = ops.size();
    vector<char>S;

    int i = 0;

    // first two real operations
    for (; i < n; i++) {
        char op = ops[i];
        while (!S.empty() && (
                    ((op == 'G' || op == 'D') &&  (S.back() == 'G' || S.back() == 'D'))
                    || ((op == 'L' || op == 'P') && (S.back() == 'L' || S.back() == 'P'))))
            S.pop_back();
        if (S.size() == 2)
            break;
        else
            S.push_back(op);
    }

    if (S.size() < 2) {
        string result;
        for (char c : S)
            result.push_back(c);
        return result;
    }

    // then the rest in diagonal mode
    char vertical_mode = (S[0] == 'G' || S[1] == 'G') ? 'G' : 'D';
    char horizontal_mode = (S[0] == 'L' || S[1] == 'L') ? 'L' : 'P';
    for (; i < n; i++) {
        char op = ops[i];
        if (op == 'G' && vertical_mode == 'G')
            continue;
        if (op == 'D' && vertical_mode == 'D')
            continue;
        if (op == 'L' && horizontal_mode == 'L')
            continue;
        if (op == 'P' && horizontal_mode == 'P')
            continue;

        if (op == 'G')
            vertical_mode = 'G';
        else if (op == 'D')
            vertical_mode = 'D';
        else if (op == 'L')
            horizontal_mode = 'L';
        else if (op == 'P')
            horizontal_mode = 'P';

        if (S.size() > 2 && ((S.back() == 'G' && op == 'D') || (S.back() == 'D' && op == 'G') || (S.back() == 'L' && op == 'P') || (S.back() == 'P' && op == 'L')))
            S.pop_back();
        else
            S.push_back(op);
    }

    string result;
    for (char c : S)
        result.push_back(c);
    return result;
}

vector<int> perm_mult(const vector<int>& p1, const vector<int>& p2) {
    int n = p1.size();
    vector<int> result(n);
    for (int i = 0; i < n; i++)
        result[i] = p2[p1[i]];
    return result;
}

vector<int> perm_pow(const vector<int>& b, int e) {
    if (e == 1)
        return b;
    auto h = perm_pow(b, e/2);
    auto hh = perm_mult(h, h);
    if (e % 2 == 1)
        return perm_mult(hh, b);
    else
        return hh;
}

/* void matrix_print(const vector<vector<int>>& M) { */
/*     cerr << "====" << endl; */
/*     for (auto& row : M) { */
/*         for (auto item : row) */
/*             cerr << item << " "; */
/*         cerr << endl; */
/*     } */
/*     cerr << "====" << endl; */
/* } */

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    vector<string>V(n);
    for (auto& r : V)
        cin >> r;

    int k;
    string ops;
    cin >> k >> ops;

    ops = op_seq_reduce(ops);

    if (ops.length() <= 16) {
        for (char op : ops)
            one_move(V, n, m, op);
    }
    else {
        one_move(V, n, m, ops[0]);
        one_move(V, n, m, ops[1]);
        string period = ops.substr(2, 4);

        vector<vector<int>> M(n, vector<int>(m));
        map<int, pair<int,int>> Poss;
        int perm_len = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (V[i][j] == '.')
                    M[i][j] = -1;
                else {
                    Poss[perm_len] = {i, j};
                    M[i][j] = perm_len++;
                }

        one_move_numeric(M, n, m, ops[2]);
        one_move_numeric(M, n, m, ops[3]);
        one_move_numeric(M, n, m, ops[4]);
        one_move_numeric(M, n, m, ops[5]);

        vector<int>Perm(perm_len);

        for (int i = 0, id = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (V[i][j] != '.')
                    Perm[M[i][j]] = id++;

        int periods = (ops.length() - 2) / 4;
        Perm = perm_pow(Perm, periods);

        vector<string> Vold(V);
        for (int id = 0; id < perm_len; id++) {
            auto old_pos = Poss[id];
            auto new_pos = Poss[Perm[id]];
            V[new_pos.first][new_pos.second] = Vold[old_pos.first][old_pos.second];
        }

        for (int i = 2 + 4*periods; i < (int)ops.length(); i++)
            one_move(V, n, m, ops[i]);
    }

    for (auto& s : V)
        cout << s << "\n";




    return 0;
}