from collections import deque
def is_valid(x, y, n, m):
    return 0 <= x < n and 0 <= y < m
def bfs(table, n, m):
    dx = [0, 1, 0, -1]
    dy = [1, 0, -1, 0]
    start = ((0, 0), (0, 1))
    end = ((n-1, m-1), (n-1, m))
    queue = deque([start])
    visited = set([start])
    while queue:
        current = queue.popleft()
        transformations = 0
        if current == end:
            return transformations
        for i in range(4):
            next_config = []
            for j in range(2):
                x, y = current[j]
                new_x = x + dx[i]
                new_y = y + dy[i]
                if is_valid(new_x, new_y, n, m) and table[new_x][new_y] == '.':
                    next_config.append((new_x, new_y))
                else:
                    next_config = []
                    break
            if next_config and tuple(next_config) not in visited:
                visited.add(tuple(next_config))
                queue.append(tuple(next_config))
                transformations += 1
    return -1
n, m = map(int, input().split())
table = [input() for _ in range(n)]
result = bfs(table, n, m)
print(result)