#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

#define ll long long

ll n, m, di[]{1, -1, 0, 0}, dj[]{0, 0, 1, -1}, di1[]{-1, -1, 1, 1}, dj1[]{-1, 1, -1, 1};
vector<ll> ans[300001];
string s;
vector<bool> a[300001];

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

void bfs() {
    queue<pair<ll, ll>> q;
    q.push({1, 1});
    ans[1][1] = 0;

    while (!q.empty()) {
        pair<ll, ll> p = q.front();
        q.pop();
        ll i = p.first, j = p.second;

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

int main() {
    cin >> n >> m;
    for (ll i = 0; i < n; i++) {
        cin >> s;
        ans[i].resize(m);
        for (ll j = 0; j < m; j++) {
            ans[i][j] = 999999999;
            a[i].push_back(s[j] != '#');
        }
    }
    bfs();
    cout << (ans[n - 1][m - 1] == 999999999 ? -1 : ans[n - 1][m - 1]);
    return 0;
}