// BEST TEACHERS: NAZAROV SHARAF and USMONZODA SOMON
//                littbird           another_love
#include <bits/stdc++.h>
#define ONLINE_JUDGE
#define fi first
#define se second
#define pb push_back
#define yes cout << "YES"
#define no cout << "NO"
#define int long long
#define in insert
#define endl '\n'
#define ld long double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
void solve() {
    int n,m;
    cin >> n >> m ;
    vector< vector<int> >used(n,vector<int>(m));
    vector< vector<int> >ans(n,vector<int>(m));
    used[0][0] = 1;
    used[0][1] = 1;
    used[1][0] = 1;
    for(int i=0;i < n;i++) {
        for(int j = 0;j < m;j ++) {
            char x;
            cin >> x;
            if(x == '#') {
                used[i][j] = 1;
            }
        }
    }
    queue< pair<int,int> >q;
    q.push(make_pair(1,1));
    while(!q.empty()){
        auto u = q.front();
        q.pop();
        int i = u.fi, j = u.se;
        used[i][j] = 1;
        if(i + 1 < n and !used[i + 1][j]){
            q.push(make_pair(i + 1,j));
            ans[i + 1][j] = ans[i][j] + 1;
        }
        if(j + 1 < m and !used[i][j + 1]){
            q.push(make_pair(i,j + 1));
            ans[i][j + 1] = ans[i][j] + 1;
        }
        if(j - 1 >= 0 and !used[i][j - 1]){
            q.push(make_pair(i ,j - 1));
            ans[i][j - 1] = ans[i][j] + 1;
        }
        if(i - 1 >= 0 and !used[i - 1][j]){
            q.push(make_pair(i - 1,j));
            ans[i - 1][j] = ans[i][j] + 1;
        }
    }
    if(!used[n - 1][m - 1]) {
        cout << -1;
    }
    else {
        cout << ans[n - 1][m - 1] * 3 - 1;
    }
}
signed main() {
#ifndef ONLINE_JUDGE
    // For getting input from input.txt file
    freopen("input.txt", "r", stdin);
    // Printing the Output to output.txt file
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int tt = 1;
   // cin >> tt;
    for (int q = 1; q <= tt; q++) {
        solve();
        cout << endl;
    }
}
