#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n,m,ex,ey;
vector<vector<bool> > t;

int lenar (int x, int y, int cost) {
    if (x == ex && y == ey) return cost;
    int cost1=INT_MAX,cost2=INT_MAX,cost3=INT_MAX;
    if (y < m-2 && x < n-2 && t[x+1][y+2] && t[x+2][y+1] &&t[x+2][y+2])
        cost1 = lenar (x+1, y+1, cost+5);
    if (y < m-2 && x < n-1 && t[x][y+2] && t[x+1][y+2])
        cost2 = lenar (x, y+1, cost+4);
    if (x < n-2 && y < m-1 && t[x+2][y+1] && t[x+2][y])
        cost3 = lenar (x+1, y, cost+4);
    return min(cost1,min(cost2,cost3));
}

int main(){
    cin >> n >> m;
    ex = n-2;
    ey = m-2;
    t.resize(n, vector<bool> (m, true));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            char a;
            cin >> a;
            if (a == '#')
                t[i][j] = false;
        }
    }
    int ans = lenar (0,0,0);
    cout << (ans == INT_MAX ? -1 :ans );
    return 0;
}
