from collections import deque

def is_valid(x, y, n, m):
    return 0 <= x < n and 0 <= y < m

def solve_slime_escape(n, m, table):
    start = (0, 0)
    end = (n - 2, m - 2)
    
    # Initialize the distance array with -1 (unvisited)
    distance = [[-1] * m for _ in range(n)]
    
    # Possible transformations from each position
    transforms = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    
    # Initialize the BFS queue
    queue = deque()
    queue.append(start)
    distance[start[0]][start[1]] = 0
    
    while queue:
        x, y = queue.popleft()
        
        # Check if we reached the end
        if (x, y) == end:
            return distance[x][y]
        
        # Try all possible transformations
        for dx, dy in transforms:
            new_x, new_y = x + dx, y + dy
            
            if is_valid(new_x, new_y, n, m) and distance[new_x][new_y] == -1 and table[new_x][new_y] != '#':
                distance[new_x][new_y] = distance[x][y] + 1
                queue.append((new_x, new_y))
    
    # Slime cannot reach the end
    return -1

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

result = solve_slime_escape(n, m, table)
print(result)
