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
def simul(b, c):
    if c == 'U':
        nb = [[] for _ in range(n)]
        for i in range(m):
            nu = []
            nd = []
            for j in range(n):
                if b[j][i] != '.':
                    nu.append(b[j][i])
                else:
                    nd.append(b[j][i])
            nn = nu + nd
            for j in range(n):
                nb[j].append(nn[j])
        return nb
    elif c == 'D':
        rec = simul(b[::-1], 'U')
        return rec[::-1]
    elif c == 'L':
        nb = []
        for l in b:
            nl = []
            nr = []
            for cc in l:
                if cc != '.':
                    nl.append(cc)
                else:
                    nr.append(cc)

            nb.append(nl + nr)
        return nb
    elif c == 'R':
        nb = []
        for l in b:
            nl = []
            nr = []
            for cc in l:
                if cc != '.':
                    nl.append(cc)
                else:
                    nr.append(cc)

            nb.append(nr + nl)
        return nb
    else:
        assert False

import random

n, m = map(int, input().split())
b = [list(input().strip()) for _ in range(n)]
#bcopy = b

uvw = int(input())
#s = ''.join(random.choice('GDPR') for _ in range(uvw))
s = input().strip()
s = s.replace('G','U').replace('P','R')


#for c in s:
#    bcopy = simul(bcopy, c)

op = {'U':'D', 'D':'U', 'L':'R','R':'L'}

b = simul(b, s[0])

st = []

def add(c):
    if st == []:
        st.append(c)
    elif c == st[-1]:
        pass
    elif c == op[st[-1]]:
        st.pop()
        add(c)
    elif len(st) == 1:
        st.append(c)
    elif st[-2] == c:
        pass
    else:
        assert st[-2] == op[c]
        st.append(c)
    
for c in s[1:]:
    add(c)
    #print(st)

for i in range(7):
    if st:
        b = simul(b, st.pop(0))
while len(st) % 4 != 0:
    b = simul(b, st.pop(0))

def e(q, r):
    n = len(q)
    out = list(range(n))

    while r:
        if r % 2:
            out = [out[q[i]] for i in range(n)]
        q = [q[q[i]] for i in range(n)]
        r //= 2
    return out

if st:
    rep = len(st) // 4
    ops = st[:4]

    for i in range(rep):
        assert st[4 * i: 4 * i + 4] == ops


    dd = []
    ct = 0
    nex = []
    bb = []
    for l in b:
        nl = []
        for c in l:
            if c == '.':
                nl.append('.')
            else:
                dd.append(c)
                nl.append(ct)
                ct += 1
        bb.append(nl)

    cc = bb
    for c in ops:
        cc = simul(cc, c)

    p = [-1] * ct
    for i in range(n):
        for j in range(m):
            if cc[i][j] == '.':
                assert bb[i][j] == '.'
            else:
                p[bb[i][j]] = cc[i][j]

    q = e(p, rep)
    for i in range(n):
        for j in range(m):
            if bb[i][j] == '.':
                assert b[i][j] == '.'
            else:
                assert b[i][j] != '.'
                b[i][j] = dd[q[bb[i][j]]]

def cout(uu):
    for l in uu:
        print(''.join(l))
        
cout(b)
#cout(bcopy)
#assert b == bcopy