import sys input = sys.stdin.readline from collections import deque H,W=list(map(int,input().split())) MAP=[input().strip() for i in range(H)] ANS=H+W D=[[1<<30]*W for i in range(H)] D[0][0]=0 Q=deque() Q.append((0,0)) while Q: x,y=Q.popleft() for z,w in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]: if 0<=zD[x][y]+1: Q.append((z,w)) D[z][w]=D[x][y]+1 ANS=min(ANS,D[H-1][W-1]) X=[[0]*W for i in range(H)] X[0][0]=1 Q=[(0,0)] while Q: x,y=Q.pop() for z,w in [(x+1,y),(x,y+1)]: if 0<=z=YX-1: ANS=min(ANS,H+W-1) if XY>=YY-1: ANS=min(ANS,H+W-1) print(ANS)