import queue

INF = 10 ** 10

H, W = map(int, input().split())

maze = [[-1] * (W+2)]
for _ in range(H):
    maze.append( [-1] + list( map(lambda c: INF if c == '.' else -1, input()) ) + [-1] )
maze.append([-1] * (W+2))

bfs_q = queue.Queue()
bfs_q.put( (1, 1) )
maze[1][1] = 0

while not bfs_q.empty():
    y, x = bfs_q.get()
    for dy, dx in ((-1, 0), (1, 0), (0, 1), (0, -1)):
        if maze[y+dy][x+dx] > maze[y][x] + 1:
            maze[y+dy][x+dx] = maze[y][x] + 1
            bfs_q.put( (y+dy, x+dx) )

print(maze[H][W])