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
def print_grid(grid):
    for row in grid:
        for c in row:
            print(c, end='')
        print()

def remove_duplicates(l):
    removed = []
    i = 0
    while i < len(l):
        removed.append(l[i])
        i += 1
        while i < len(l) and l[i] == removed[-1]:
            i += 1
    return removed

def move_left(grid):
    for row in grid:
        i = 0
        for j, c in enumerate(row):
            if c == 'C' or c == 'B':
                row[j] = '.'
                row[i] = c 
                i += 1

def move_right(grid):
    for row in grid:
        i = m - 1
        for j, c in list(enumerate(row))[::-1]:
            if c == 'C' or c == 'B':
                row[j] = '.'
                row[i] = c 
                i -= 1

def move_up(grid):
    for col in range(m):
        next_free = 0
        for i in range(n):
            c = grid[i][col]
            if c == 'C' or c == 'B':
                grid[i][col] = '.'
                grid[next_free][col] = c 
                next_free += 1

def move_down(grid):
    for col in range(m):
        next_free = n - 1
        for i in range(n)[::-1]:
            c = grid[i][col]
            if c == 'C' or c == 'B':
                grid[i][col] = '.'
                grid[next_free][col] = c 
                next_free -= 1


n, m = [int(x) for x in input().strip().split(" ")]

grid = [['.'] * m for _ in range(n)]

for i in range(n):
    row = list(input())
    for j, c in enumerate(row):
        grid[i][j] = c

instuction_len = int(input())
instructions = list(input())

instructions = remove_duplicates(instructions)
# print(instructions)

for ins in instructions:
    if ins == 'D':
        move_down(grid)
    elif ins == 'G':
        move_up(grid)
    elif ins == 'L':
        move_left(grid)
    else:
        move_right(grid)

print_grid(grid)

# gd_encountered = False
# lp_encountered = False

# print_grid(grid)
# print()



# move_down(grid)
# move_left(grid)

# print_grid(grid)
# print()

# for _ in range(4):
#     move_down(grid)
#     move_right(grid)
#     move_up(grid)
#     move_left(grid)

#     print_grid(grid)
#     print()

# new_instructions = []
# for ins in instructions:
#     if new_instructions and ins == new_instructions[-1]:
#         continue



#     if ins == 'G' or ins == 'D':
#         gd_encountered = True
#     else:
#         lp_encountered = True