def moving(row, col, direction): if direction == "up": return row - 1, col if direction == "down": return row + 1, col if direction == "right": return row, col + 1 if direction == "left": return row, col - 1 rows, cols = [int(x) for x in input().split()] matrix = [] player_row = 0 player_col = 0 for row in range(rows): inner_list = input().split() for col in range(cols): if inner_list[col] == 'B': player_row = row player_col = col matrix.append(inner_list) touched_opponents = 0 moves = 0 while True: line = input() if line == 'Finish' or touched_opponents == 3: break direction = line next_row, next_col = moving(player_row, player_col, direction) if next_row < 0 or next_col < 0 or next_row >= rows or next_col >= cols: next_row, next_col = player_row, player_col if matrix[next_row][next_col] == 'O': next_row, next_col = player_row, player_col continue if matrix[next_row][next_col] == '-': moves += 1 if matrix[next_row][next_col] == 'P': moves += 1 touched_opponents += 1 matrix[player_row][player_col] = '-' print("Game over!") print(f'Touched opponents: {touched_opponents} Moves made: {moves}')