from collections import deque h, w = map(int, input().split()) a = [list(input()) for _ in range(h)] # どのように行っても最短路 dij = [(1, 0), (0, 1)] ans = 0 stack = [(1, 0, 0)] while stack: t, i, j = stack.pop() for di, dj in dij: ni = i+di nj = j+dj nt = t if not(0<=ni