def main():
    from collections import deque

    h, w = map(int, input().split())

    s = []
    sr, sc = -1, -1
    for r in range(h):
        _s = input()
        s.append(_s)

        if sr == -1:
            col = _s.find('.')
            if col >= 0:
                sr = r
                sc = col

    drdc = (1, 0), (-1, 0), (0, 1), (0, -1)

    q = deque()
    q.append((sr, sc, 0))

    dist = [[-1] * w for _ in range(h)]
    dist[sr][sc] = 0

    while q:
        r, c, d = q.popleft()
        for dr, dc in drdc:
            nr = r + dr
            if nr < 0 or h <= nr:
                continue
            nc = c + dc
            if nc < 0 or w <= nc:
                continue
            if s[nr][nc] == '#':
                continue
            if dist[nr][nc] != -1:
                continue
            dist[nr][nc] = d + 1
            q.append((nr, nc, d + 1))

    sr, sc = r, c

    q = deque()
    q.append((sr, sc, 0))

    dist = [[-1] * w for _ in range(h)]
    dist[sr][sc] = 0

    while q:
        r, c, d = q.popleft()
        for dr, dc in drdc:
            nr = r + dr
            if nr < 0 or h <= nr:
                continue
            nc = c + dc
            if nc < 0 or w <= nc:
                continue
            if s[nr][nc] == '#':
                continue
            if dist[nr][nc] != -1:
                continue
            dist[nr][nc] = d + 1
            q.append((nr, nc, d + 1))

    print(d)


if __name__ == '__main__':
    main()
