from collections import deque

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

def can_escape(n, m, table):
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    visited = [[False] * m for _ in range(n)]
    slime = [[-1] * m for _ in range(n)]

    q = deque()

    # Ищем начальную позицию слайма
    for i in range(n - 1):
        for j in range(m - 1):
            if all(table[i + k][j + l] == '.' for k in range(2) for l in range(2)):
                slime[i][j] = 0
                q.append((i, j))

    while q:
        x, y = q.popleft()

        if x == n - 2 and y == m - 2:
            return slime[x][y]

        for k in range(4):
            nx, ny = x + dx[k], y + dy[k]

            if is_valid(nx, ny, n, m) and not visited[nx][ny]:
                if all(table[nx + i][ny + j] == '.' for i in range(2) for j in range(2)):
                    slime[nx][ny] = slime[x][y]
                    q.appendleft((nx, ny))
                else:
                    slime[nx][ny] = slime[x][y] + 1
                    q.append((nx, ny))

        visited[x][y] = True

    return -1

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

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