import sys

input = lambda: sys.stdin.readline().rstrip()

n, m = map(int, input().split())
board = []
r, b = [0, 0], [0, 0]  # 빨간색 파란색 구슬의 현재 위치 x, y

for i in range(n):
    line = list(input())

    for j in range(m):
        if line[j] == 'R':
            r = [j, i]
            line[j] = '.'
        elif line[j] == 'B':
            b = [j, i]
            line[j] = '.'

    board.append(line)

print("R:", r, ", B:", b)
for i in range(n):
    print(board[i])


def tilt_left_right(marble, other, is_right):
    y = marble[1]
    other_marble_count = 0

    if is_right:
        x_range = range(marble[0] + 1, m)
    else:
        x_range = range(marble[0] - 1, -1, -1)

    for x in x_range:
        if other[0] == x and other[1] == y:
            other_marble_count = 1  # 벽 사이에 다른 구슬이 낀 경우

        if board[y][x] == '#':
            if is_right:
                return [x - other_marble_count - 1, y]
            else:
                return [x + other_marble_count + 1, y]

        if board[y][x] == 'O':
            return [x, y]

    return [-1, -1]  # TODO Exception


def tilt_up_down(marble, other, is_down):
    x = marble[0]
    other_marble_count = 0

    if is_down:
        y_range = range(marble[1] + 1, n)
    else:
        y_range = range(marble[1] - 1, -1, -1)

    for y in y_range:
        if other[0] == x and other[1] == y:
            other_marble_count = 1

        if board[y][x] == '#':
            if is_down:
                return [x, y - other_marble_count - 1]
            else:
                return [x, y + other_marble_count + 1]

        if board[y][x] == 'O':
            return [x, y]

    return [-1, -1]  # TODO Exception


def is_exit(marble):
    return board[marble[1]][marble[0]] == 'O'


def dfs(red, blue, count):
    print("red:", red)
    print("blue:", blue)
    print("count:", count)

    if count > 10:
        return count

    result = 11

    # 왼쪽으로 기울이기
    next_red = tilt_left_right(red, blue, False)
    next_blue = tilt_left_right(blue, red, False)

    if not is_exit(next_blue):
        if is_exit(next_red):
            result = min(result, count)
        else:
            result = min(result, dfs(next_red, next_blue, count + 1))

    # 오른쪽으로 기울이기
    tilt_left_right(red, blue, True)
    tilt_left_right(blue, red, True)

    if not is_exit(next_blue):
        if is_exit(next_red):
            result = min(result, count)
        else:
            result = min(result, dfs(next_red, next_blue, count + 1))

    # 위쪽으로 기울이기
    tilt_up_down(red, blue, False)
    tilt_up_down(blue, red, False)

    if not is_exit(next_blue):
        if is_exit(next_red):
            result = min(result, count)
        else:
            result = min(result, dfs(next_red, next_blue, count + 1))

    # 아래쪽으로 기울이기
    tilt_up_down(red, blue, True)
    tilt_up_down(blue, red, True)

    if not is_exit(next_blue):
        if is_exit(next_red):
            result = min(result, count)
        else:
            result = min(result, dfs(next_red, next_blue, count + 1))

    return result


answer = dfs(r, b, 0)
if answer > 10:
    print(-1)
else:
    print(answer)
