#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <limits.h>

#define ll long long
#define MAX_SIZE 300001

ll n, m;
ll di[] = {1, -1, 0, 0};
ll dj[] = {0, 0, 1, -1};
ll di1[] = {-1, -1, 1, 1};
ll dj1[] = {-1, 1, -1, 1};
ll ans[MAX_SIZE][MAX_SIZE];
bool a[MAX_SIZE][MAX_SIZE];

bool ok(ll i, ll j) {
    if (i > 0 && j > 0 && i < n && j < m) {
        return (a[i][j] && a[i - 1][j] && a[i][j - 1] && a[i - 1][j - 1]);
    } else return false;
}

void dfs(ll i, ll j) {
    for (ll k = 0; k < 4; k++) {
        ll ci = i + di[k], cj = j + dj[k];
        if (ok(ci, cj) && ans[ci][cj] > ans[i][j] + 4) {
            ans[ci][cj] = ans[i][j] + 4;
            dfs(ci, cj);
        }
    }
    for (ll k = 0; k < 4; k++) {
        ll ci = i + di1[k], cj = j + dj1[k];
        if (ok(ci, cj) && ans[ci][cj] > ans[i][j] + 5) {
            ans[ci][cj] = ans[i][j] + 5;
            dfs(ci, cj);
        }
    }
}

int main() {
    scanf("%lld %lld", &n, &m);
    char s[MAX_SIZE];
    for (ll i = 0; i < n; i++) {
        scanf("%s", s);
        for (ll j = 0; j < m; j++) {
            ans[i][j] = LLONG_MAX;
            a[i][j] = (s[j] != '#');
        }
    }
    ans[1][1] = 0;
    dfs(1, 1);
    printf("%lld", (ans[n - 1][m - 1] == LLONG_MAX ? -1 : ans[n - 1][m - 1]));
    return 0;
}