from collections import deque

ll = int
di = [1, -1, 0, 0]
dj = [0, 0, 1, -1]
di1 = [-1, -1, 1, 1]
dj1 = [-1, 1, -1, 1]

n, m = 0, 0
ans = []
s = ""
a = []

def ok(i, j):
    return i > 0 and j > 0 and i < n and j < m and a[i][j] and a[i-1][j] and a[i][j-1] and a[i-1][j-1]

def dfs(i, j):
    for k in range(4):
        ci, cj = i + di[k], j + dj[k]
        if ok(ci, cj) and ans[ci][cj] > ans[i][j] + 4:
            ans[ci][cj] = ans[i][j] + 4
            dfs(ci, cj)
    for k in range(4):
        ci, cj = i + di1[k], j + dj1[k]
        if ok(ci, cj) and ans[ci][cj] > ans[i][j] + 5:
            ans[ci][cj] = ans[i][j] + 5
            dfs(ci, cj)

def main():
    global n, m, ans, a
    n, m = map(int, input().split())
    for i in range(n):
        s = input()
        ans.append([999999999] * m)
        a.append([c != '#' for c in s])
    ans[1][1] = 0
    dfs(1, 1)
    print(-1 if ans[n-1][m-1] == 999999999 else ans[n-1][m-1])

if _name_ == "_main_":
    main()