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

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

def main():
    n, m = map(int, input().split())
    a = []
    ans = [[float('inf')] * m for _ in range(n)]

    for i in range(n):
        row = input().strip()
        a.append([cell != '#' for cell in row])

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

    ans[1][1] = 0
    dfs(a, ans, 1, 1, n, m, di, dj, di1, dj1)
    result = ans[n-1][m-1]
    print(-1 if result == float('inf') else result)

if _name_ == "_main_":
    main()