from collections import deque

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

def can_escape(n, m, table):
    directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
    
    # Initialize the queue with the starting position
    queue = deque([(0, 0, 1)])  # (x, y, moves)
    
    while queue:
        x, y, moves = queue.popleft()
        
        if (x, y) == (n - 1, m - 1):
            return moves  # Reached the target
        
        for dx, dy in directions:
            new_x, new_y = x + dx, y + dy
            
            if is_valid(new_x, new_y, n, m, table):
                # Mark the cell as visited
                table[new_x][new_y] = '#'
                queue.append((new_x, new_y, moves + 1))
    
    return -1  # Unable to reach the target

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

# Find the minimum moves to escape
result = can_escape(n, m, table)
print(result)
