H,W = map(int,input().split())
visited = [[False for i in range(W+2)] for j in range(H+2)]                                     # ノードの訪問済みフラグ
graph = [['#']+[i for i in input()]+['#'] for j in range(H)]
graph = [['#' for i in range(W+2)]]+graph+[['#' for i in range(W+2)]]
 
import copy
# dfs:ノードvについて、エッジで接続した未訪問のノードを浅いほうから訪問(LILO)
def dfs(r,c,goal):
    visited = [[False for i in range(W+2)] for j in range(H+2)]
    step = 0
    queue = [[r,c]]
    if graph[r][c]=='#':
      return -1
    queue_2 = []
    while len(queue) != 0:
        step += 1
        for now in queue:
          visited[now[0]][now[1]] = True
          for next in [[1,0],[0,1],[-1,0],[0,-1]]:
            if visited[now[0]+next[0]][now[1]+next[1]] == True:continue
            if graph[now[0]+next[0]][now[1]+next[1]] == '#':continue
            if [now[0]+next[0],now[1]+next[1]] == goal:return step
            if [now[0]+next[0],now[1]+next[1]] in queue_2:continue
            queue_2.append([now[0]+next[0],now[1]+next[1]])
        queue.clear()
        queue = copy.deepcopy(queue_2)
        queue_2.clear()
    return -1

print(max(dfs(1,1,[H,W]),dfs(H,1,[1,W])))