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


import sys

def main():
    data = sys.stdin
    rows, cols = map(int, data.readline().split())
    grid = [data.readline().strip() for _ in range(rows)]
    x, y, theta = map(int, data.readline().split())
    n = int(data.readline())
    
    packets = {}
    for _ in range(n):
        parts = data.readline().split()
        s = int(parts[0])
        cmd = parts[1]
        packets[s] = cmd
    
    blocked = 0
    
    for s in sorted(packets.keys()):
        cmd = packets[s]
        
        if cmd == 'L':
            theta = (theta + 90) % 360
        elif cmd == 'R':
            theta = (theta - 90) % 360
        elif cmd == 'F':
            if theta == 0:
                nx, ny = x + 1, y
            elif theta == 90:
                nx, ny = x, y + 1
            elif theta == 180:
                nx, ny = x - 1, y
            else:
                nx, ny = x, y - 1
            
            if 0 <= nx < cols and 0 <= ny < rows:
                row_in_grid = rows - 1 - ny
                if grid[row_in_grid][nx] == '.':
                    x, y = nx, ny
                else:
                    blocked += 1
            else:
                blocked += 1
        elif cmd == 'B':
            if theta == 0:
                nx, ny = x - 1, y
            elif theta == 90:
                nx, ny = x, y - 1
            elif theta == 180:
                nx, ny = x + 1, y
            else:
                nx, ny = x, y + 1
            
            if 0 <= nx < cols and 0 <= ny < rows:
                row_in_grid = rows - 1 - ny
                if grid[row_in_grid][nx] == '.':
                    x, y = nx, ny
                else:
                    blocked += 1
            else:
                blocked += 1
    
    print(x, y, theta, blocked)

main()