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
import sys

n, m = [int(x) for x in sys.stdin.readline().split(" ") if x != "\n"]

rows = []
for i in range(n):
    row = []
    for ch in sys.stdin.readline():
        if ch != "\n":
            row.append(ch)
    rows.append(row)

k = int(sys.stdin.readline())
moves = sys.stdin.readline()

for move in moves:
    if move == "G":
        for i in range(m):
            non_empty = []
            for j in range(n):
                if rows[j][i] != ".":
                    non_empty.append(rows[j][i])
                    rows[j][i] = "."

            for j in range(len(non_empty)):
                rows[j][i] = non_empty[j]

    elif move == "D":
        for i in range(m):
            non_empty = []
            for j in range(n):
                if rows[j][i] != ".":
                    non_empty.append(rows[j][i])
                    rows[j][i] = "."

            non_empty = non_empty[::-1]
            for j in range(len(non_empty)):
                rows[n - 1 - j][i] = non_empty[j]

    elif move == "L":
        for j in range(n):
            non_empty = []
            for i in range(m):
                if rows[j][i] != ".":
                    non_empty.append(rows[j][i])
                    rows[j][i] = "."

            for i in range(len(non_empty)):
                rows[j][i] = non_empty[i]

    elif move == "P":
        for j in range(n):
            non_empty = []
            for i in range(m):
                if rows[j][i] != ".":
                    non_empty.append(rows[j][i])
                    rows[j][i] = "."

            non_empty = non_empty[::-1]

            for i in range(len(non_empty)):
                rows[j][m - 1 - i] = non_empty[i]

for row in rows:
    print("".join(row))