Загрузка данных


import sys
input = sys.stdin.readline

rows, cols = map(int, input().split())

grid = []
for _ in range(rows):
    grid.append(input().strip())

x, y, theta = map(int, input().split())
n = int(input())

packets = {}
for _ in range(n):
    s, cmd = input().split()
    packets[int(s)] = cmd

dirs = {0: (1, 0), 90: (0, 1), 180: (-1, 0), 270: (0, -1)}
blocked = 0

for s in sorted(packets):
    cmd = packets[s]
    if cmd == 'L':
        theta = (theta + 90) % 360
    elif cmd == 'R':
        theta = (theta - 90) % 360
    else:
        dx, dy = dirs[theta]
        if cmd == 'B':
            dx, dy = -dx, -dy
        nx, ny = x + dx, y + dy
        if 0 <= nx < cols and 0 <= ny < rows and grid[rows - 1 - ny][nx] == '.':
            x, y = nx, ny
        else:
            blocked += 1

print(x, y, theta, blocked)