H, W = map(int, input().split()) S = [input() for _ in range(H)] L = [(0,0)] Seen = [[False] * W for _ in range(H)] dx = [1, 0] dy = [0, 1] S_height = 0 S_width = 0 while len(L): (x, y) = L.pop() if(x == H - 1 and y == W - 1): print(H + W - 2) exit() S_height = max(x, S_height) S_width = max(y, S_width) for k in range(2): x2 = x + dx[k] y2 = y + dy[k] if(x2 < H and y2 < W and S[x2][y2] != "#" and not Seen[x2][y2]): Seen[x2][y2] = True L.append((x2, y2)) L = [(H - 1,W - 1)] Seen = [[False] * W for _ in range(H)] dx = [-1, 0] dy = [0, -1] G_height = H - 1 G_width = W - 1 while len(L): (x, y) = L.pop() G_height = min(x, G_height) G_width = min(y, G_width) for k in range(2): x2 = x + dx[k] y2 = y + dy[k] if(0 <= x2 and 0 <= y2 and S[x2][y2] != "#" and not Seen[x2][y2]): Seen[x2][y2] = True L.append((x2, y2)) if(G_height - S_height <= 1 or G_width - G_height <= 1): print(H + W -1) else: print(H + W)