from collections import deque

H, W = map(int, input().split())
S = [input() for _ in range(H)]

def solve(s):
    offset = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    visited = [[-1] * W for _ in range(H)]
    visited[0][0] = 0
    
    if s[0] < W and s[1] < H:
        if S[s[1]][s[0]] != "#":
            visited[s[1]][s[0]] = 1
        else:
            return visited

    if s[0] >= W or s[1] >= H:
        return visited
    
    q = deque()
    q.append(s)

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

        for u, v in offset:
            if not (0 <= x + u < W) or not (0 <= y + v < H):
                continue

            if visited[y + v][x + u] > -1 or S[y + v][x + u] == "#":
                continue

            visited[y + v][x + u] = visited[y][x] + 1
            q.append((x + u, y + v))
            
    return visited

ans_1 = (0, 0, 0)
for y, v in enumerate(solve((0, 1))):
    for x, c in enumerate(v):
        if c >= ans_1[2]:
            ans_1 = (x, y, c)
        
ans_2 = (0, 0, 0)
for y, v in enumerate(solve((1, 0))):
    for x, c in enumerate(v):
        if c >= ans_2[2]:
            ans_2 = (x, y, c)
        
if ans_1[0] == ans_2[0] and ans_1[1] == ans_2[1]:
    print(min(ans_1[2], ans_2[2]))
else:
    print(ans_1[2] + ans_2[2])
