def main():
    import networkx as nx
    import numpy as np
    h,w = map(int, input().split())
    s = [list(input()) for i in range(h)]
    dic = {'.':0, '#':1}
    g = [[dic[i] for i in _s] for _s in s]
    graph = nx.grid_graph(dim=[h, w])
    rm_node = np.array(g)

    for (u, _) in graph.nodes(data=True):
        if rm_node[u]:
            graph.remove_node(u)
    try:
        ans1 = nx.dijkstra_path(graph, (0, 0), (h-1, w-1))
    except nx.NetworkXNoPath:
        ans1 = []
    try:
        ans2 = nx.dijkstra_path(graph, (h-1, 0), (0, w-1))
    except nx.NetworkXNoPath:
        ans2 = []
    print(max(max(len(ans1),len(ans2))-1,0))

if __name__ == '__main__':
    main()
