from collections import deque

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

start = [(0, 0), (0, 1), (1, 0), (1, 1)]
target = [(n - 2, m - 2), (n - 2, m - 1), (n - 1, m - 2), (n - 1, m - 1)]

visited = set()
queue = deque([(start, 0)])
neighbors = [(-1, 0), (1, 0), (0, -1), (0, 1)]

while queue:
    current, transformations = queue.popleft()

    if current == target:
        print(transformations)
        break

    if tuple(current) in visited:
        continue

    visited.add(tuple(current))

    for dx, dy in neighbors:
        new_slime = [(x + dx, y + dy) for x, y in current]
        valid_move = True

        for x, y in new_slime:
            if table[x][y] == '#':
                valid_move = False
                break

        if valid_move:
            queue.append((new_slime, transformations + 1))

else:
    print(-1)