def main():
input = sys.stdin.readline
rows, columns = map(int, input().split())
field = [input().strip() for _ in range(rows)]
field.reverse()
x, y, theta = map(int, input().split())
n = int(input())
packets = {}
for _ in range(n):
s, command = input().split()
packets[int(s)] = command
directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
direction = theta // 90
blocked = 0
for s in sorted(packets):
command = packets[s]
if command == "L":
direction = (direction + 1) % 4
elif command == "R":
direction = (direction - 1) % 4
else:
dx, dy = directions[direction]
if command == "B":
dx, dy = -dx, -dy
nx, ny = x + dx, y + dy
if (0 <= nx < columns and 0 <= ny < rows
and field[ny][nx] == "."):
x, y = nx, ny
else:
blocked += 1
print(x, y, direction * 90, blocked)
if __name__ == "__main__":
main()
import sys
def main():
data = sys.stdin.read().split()
n = int(data[0])
total = current = longest = 0
for i in range(1, n + 1):
record = data[i]
command = record[0]
duration = int(record[1:])
if command in ("F", "B"):
total += duration
current += duration
longest = max(longest, current)
else:
current = 0
print(total, longest)
if __name__ == "__main__":
main()