import heapq
import itertools


def astar(init_pos, goal):
    passed_list = [init_pos]
    init_score = distance(passed_list) + heuristic(init_pos)
    checked = {init_pos: init_score}
    searching_heap = []
    heapq.heappush(searching_heap, (init_score, passed_list))
    while len(searching_heap) > 0:
        score, passed_list = heapq.heappop(searching_heap)
        last_passed_pos = passed_list[-1]
        if last_passed_pos == goal:
            return passed_list
        for pos in nexts(last_passed_pos):
            new_passed_list = passed_list + [pos]
            pos_score = distance(new_passed_list) + heuristic(pos)
            if pos in checked and checked[pos] <= pos_score:
                continue
            checked[pos] = pos_score
            heapq.heappush(searching_heap, (pos_score, new_passed_list))

    return []

if __name__ == "__main__":
    h, w = map(int, input().split())
    maze = [input() for j in range(h)]

    init = (0,0)
    goal = (h-1,w-1)

    def nexts(pos):
        ''' 探索中の座標から八方の座標を計算するジェネレーター'''
        wall = "#"

        # for a, b in itertools.product([' + 1', ' - 1', ''], repeat=2):
        for a, b in [["+1",""], ["-1", ""], ["", "+1"], ["", "-1"]]:
            if a or b:
                if 0 <= eval('pos[0]' + a) < h and 0 <= eval('pos[1]' + b) < w and maze[eval('pos[0]' + a)][eval('pos[1]' + b)] != wall:
                    yield (eval('pos[0]' + a), eval('pos[1]' + b))

    def heuristic(pos):
        ''' 探索中の座標からゴールまでの最短距離のスコア '''
        return ((pos[0] - goal[0]) ** 2 + (pos[1] - goal[1]) ** 2) ** 0.5

    def distance(path):
        ''' スタートから探索中の座標までの距離のスコア '''
        return len(path)

    def render_path(path):
        ''' 結果の出力 '''
        buf = [[ch for ch in l] for l in maze]

        for pos in path[1:-1]:
            buf[pos[0]][pos[1]] = "*"

        buf[path[0][0]][path[0][1]] = "s"
        buf[path[-1][0]][path[-1][1]] = "g"
        return ["".join(l) for l in buf]

    path1 = astar(init, goal)

    init = (h-1,0)
    goal = (0,w-1)

    path2 = astar(init, goal)

    if len(path1) > 0 and len(path2) > 0:
        print(max(len(path1), len(path2)) - 1)
    elif len(path1) > 0:
        print(len(path1)-1)
    elif len(path2) > 0:
        print(len(path2) - 1)