#include <bits/stdc++.h>
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define len(x) ((long long)(x).size())
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using pll = pair<ll, ll>;
template<class T>void print1d(T x,ll n=-1){if(n==-1)n=x.size();rep(i,0,n){cout<<x[i]<<' ';}cout<<'\n';}
template<class T>void print2d(T x,ll r=-1,ll c=-1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c);}
template<class T, class U>bool haskey(T mp, U key) { return mp.find(key) != mp.end(); }
template<class T, class U>bool isin(T el, U container) { return find(all(container), el) != container.end(); }
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T>bool even(T n) { return ! (n & 1); }
template<class T>bool odd(T n) { return n & 1; }
template<class T>ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template<class T>ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
ll intpow(ll a,ll n){ll p=1;while(n){if(n&1)p*=a;a*=a;n>>=1;}return p;}
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;



int main()
{
        ll H, W;
        cin >> H >> W;
        vector<vector<char> > grid(H+2, vector<char>(W+2, '.'));
        rep(i, 1, H+1) {
                rep(j, 1, W+1) {
                        cin >> grid[i][j];
                }
        }
        vllvll dist(H+2, vll(W+2, inf));

        queue<pll> q;
        q.push(pll{1, 1});
        dist[1][1] = 0;
        while (not q.empty()) {
                auto now = q.front();
                ll x = now.second;
                ll y = now.first;
                q.pop();
                if (grid[y-1][x] == '.' and dist[y-1][x] == inf and y-1 >= 1) {
                        dist[y-1][x] = dist[y][x] + 1;
                        q.push(pll{y-1, x});
                }
                if (grid[y+1][x] == '.' and dist[y+1][x] == inf and y+1 <= H) {
                        dist[y+1][x] = dist[y][x] + 1;
                        q.push(pll{y+1, x});
                }
                if (grid[y][x-1] == '.' and dist[y][x-1] == inf and x-1 >= 1) {
                        dist[y][x-1] = dist[y][x] + 1;
                        q.push(pll{y, x-1});
                }
                if (grid[y][x+1] == '.' and dist[y][x+1] == inf and x+1 <= W) {
                        dist[y][x+1] = dist[y][x] + 1;
                        q.push(pll{y, x+1});
                }
        }

        if (dist[H][W] == inf) {
                cout << -1 << endl;
                return 0;
        }
        ll mx = -inf;
        map<ll, ll> mp;
        rep(i, 1, H+1) {
                rep(j, 1, W+1) {
                        if (dist[i][j] == inf) continue;
                        chmax(mx, dist[i][j]);
                        mp[dist[i][j]]++;
                }
        }
        ll goalval = dist[H][W];

        ll ans = 0;
        for (auto x : mp) {
                ll d = x.first;
                ll freq = x.second;
                if (d == inf) continue;
                if (freq >= 2) {
                        ans += freq - 1;
                }
                if (d > goalval) ++ans;
        }

        rep(i, 1, H+1) {
                rep(j, 1, W+1) {
                        if (grid[i][j] == '.' and dist[i][j] == inf) ++ans;
                }
        }
        cout << ans << endl;
}

Measured memory_bytes: 3652