#include <bits/stdc++.h>
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define vl vector<ll>
#define pstr pair<string, string>
#define lli long long int
#define ll long long
#define ld long double
#define ull unsigned long long
#define sz size()
#define pb push_back
#define pob pop_back
#define pf push_front
#define ff first
#define ss second
#define F first
#define S second
#define mk make_pair
#define fr front()
#define PI 3.14159265358979323846
using namespace std;
const ll MOD = 1e9 + 7;
const ll N = 1e5 + 5, inf = 1e18, maxsize = 20;

ll binpow(ll a, ll b){
    if(b == 1) return a;
    ll z = binpow(a, b / 2);
    if(b % 2 == 0) return (z * z);
    else return (z * z) * a ;
}

ll binpow_m(ll a, ll b){
    if(b == 1) return a % MOD;
    ll z = binpow(a, b / 2);
    if(b % 2 == 0) return (z * z) % MOD;
    else return (z * z) % MOD * a % MOD;
}

ll lcm(ll a, ll b) {
    return (a / __gcd(a, b)) * b;
}

ll Prime(unsigned long a, unsigned long j){
   return j * j <= a ? (a % j) && Prime(a, j + 1) : 1;
}

bool isPrime(ll n){
    for(ll i = 2; i <= sqrt(n); i++){
        if(n % i == 0){
            return false;
        }
    }
    return true;
}

vector<pll> dr={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int main(/*d!ZzAsTer?*/)
{
    boost
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    ll n,m;
    cin>>n>>m;
    char c[n+1][m+1];
    ll d[n+1][m+1];
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=m;j++){
            cin>>c[i][j];
            d[i][j]=inf;
        }
    }
    queue<vl> q;
    q.push({1,1,0});
    while(!q.empty()){
        ll x=q.fr[0],y=q.fr[1],t=q.fr[2];
        q.pop();
        if(d[x][y]<=t)continue;
        d[x][y]=t;
        for(pll p:dr){
            ll x2=x+p.F,y2=y+p.S;
            if(x2<1||x2>=n||y2<1||y2>=m)continue;
            if(c[x2][y2]!='.'||c[x2][y2+1]!='.'||c[x2+1][y2]!='.'||c[x2+1][y2+1]!='.')continue;
            ll s=4;
            if(abs(p.F)+abs(p.S)==2)s++;
            if(d[x2][y2]>t+s)q.push({x2,y2,t+s});
        }
    }
    if(d[n-1][m-1]!=inf) cout<<d[n-1][m-1];
    else cout<<-1;
    return 0;
}
