H, W = map(int, input().split()) C = [] def print_mat(c): for l in c: print(l) for h in range(H): C.append(input()) sl = [[0] * W for _ in range(H)] gl = [[0] * W for _ in range(H)] sl[0][0] = 1 gl[-1][-1] = 1 max_sw = 0 max_sh = 0 for h in range(H): for w in range(W): if C[h][w] == "#": continue if h > 0 and sl[h - 1][w] != 0: sl[h][w] = 1 if max_sh < h: max_sh = h if w > 0 and sl[h][w - 1] != 0: sl[h][w] = 1 if max_sw < w: max_sw = w min_gw = W - 1 min_gh = H - 1 for h in reversed(range(H)): for w in reversed(range(W)): if C[h][w] == "#": continue if h < H - 1 and gl[h + 1][w] != 0: gl[h][w] = 1 if min_gh > h: min_gh = h if w < W - 1 and gl[h][w + 1] != 0: gl[h][w] = 1 if min_gw > w: min_gw = w ans = H + W - 2 if sl[H - 1][W - 1] == 1: print(ans) elif min_gh <= max_sh + 1 or min_gw <= max_sw + 1: print(ans + 1) else: print(ans + 2)