h,w = map(int,input().split()) s = [input()+"#" for _ in range(h)]+["#"*(w+1)] def check(t): f = [[False]*(w+1) for _ in range(h+1)] f[1][1] = True; st = [(1,1)] while st: px,py = st.pop() for dx,dy in ((-1,0),(0,-1),(0,1),(1,0)): nx,ny = px+dx,py+dy if f[nx][ny]: continue g = True for i in range(-1,2): for j in range(-1,2): g &= t[nx+i][ny+j]=="." if g: f[nx][ny] = True; st.append((nx,ny)) return f[h-2][w-2] if not check(s): exit(print(0)) for i in range(h): for j in range(w): if 0<=i<=2 and 0<=j<=2 or h-3<=i<=h-1 and w-3<=j<=w-1: continue t = [list(si) for si in s] t[i][j] = "#" if not check(t): exit(print(1)) print(2)