from collections import deque

n, m = map(int, input().split())
table = [list(input().strip()) for _ in range(n)]

def is_valid(x, y):
    return 0 <= x < n and 0 <= y < m and table[x][y] != '#'

def bfs():
    start = (0, 0, 1)  # Starting state (top-left cell, orientation)
    target = (n - 1, m - 1, 2)  # Target state (bottom-right cell, orientation)
    visited = set()

    queue = deque([(start, 0)])  # (state, steps)

    while queue:
        (x, y, orientation), steps = queue.popleft()

        if (x, y, orientation) == target:
            return steps

        if (x, y, orientation) in visited:
            continue

        visited.add((x, y, orientation))

        # Move down
        if is_valid(x + 1, y) and is_valid(x + 1, y + 1):
            new_state = (x + 1, y, orientation)
            queue.append((new_state, steps + 1))

        # Move right
        if is_valid(x, y + 1) and is_valid(x + 1, y + 1):
            new_state = (x, y + 1, orientation)
            queue.append((new_state, steps + 1))

        # Diagonal move
        if orientation == 1:
            if is_valid(x + 1, y) and is_valid(x, y + 1):
                new_state = (x + 1, y, 2)
                queue.append((new_state, steps + 1))
        else:
            if is_valid(x - 1, y) and is_valid(x, y - 1):
                new_state = (x - 1, y, 1)
                queue.append((new_state, steps + 1))

    return -1

result = bfs()
print(result)
