from collections import deque import sys input = sys.stdin.readline H, W = map(int, input().split()) C = [input().strip() for _ in range(H)] HH, WW = 2 * H - 1, 2 * W - 1 def ok(x, y): return 0 <= x < HH and 0 <= y < WW and not ( x % 2 == 0 and y % 2 == 0 and C[x // 2][y // 2] == "#" ) dist = [[-1] * WW for _ in range(HH)] dist[0][0] = 0 q = deque([(0, 0)]) while q: x, y = q.popleft() ns = [] for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)): ns.append((x + dx, y + dy)) if x % 2 == 0 and y % 2 == 0: ns += [(x + 2, y), (x - 2, y), (x, y + 2), (x, y - 2)] elif x % 2 == 1 and y % 2 == 0: ns += [(x, y + 2), (x, y - 2)] elif x % 2 == 0 and y % 2 == 1: ns += [(x + 2, y), (x - 2, y)] for nx, ny in ns: if ok(nx, ny) and dist[nx][ny] < 0: dist[nx][ny] = dist[x][y] + 1 q.append((nx, ny)) print(dist[-1][-1])