#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
//#define INF 1145141919810893364
//#define INF 810114514
#define PI 3.141592653589
typedef long long ll;
typedef pair<ll,ll> PP;
#define int ll
#define setdouble setprecision
#define REP(i,n) for(int i=0;i<(n);++i)
#define OREP(i,n) for(int i=1;i<=(n);++i)
#define RREP(i,n) for(int i=(n)-1;i>=0;--i)
#define GOODBYE do { cout << "0" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug false

class Dijkstra{
    private:
    typedef std::pair<long long,int> P;
    std::vector<std::vector<P>> G;
    int V=0,E=0;
    long long INF = (1LL<<61);
    std::priority_queue<P,std::vector<P>,std::greater<P>> que;
    
    public:
    void init(std::vector<std::pair<std::pair<int,int>,long long>> edge){
        //辺数･頂点数を決定する
        E=edge.size();
        V=0;
        for(int i=0;i<E;i++){
            V=std::max(V,edge[i].first.first+1);
            V=std::max(V,edge[i].first.second+1);
        }
        
        //辺集合を扱いやすい形式に変換する
        G.clear();
        G.resize(V);
        for(int i=0;i<E;i++){
            int from=edge[i].first.first,to=edge[i].first.second;
            long long cost=edge[i].second;
            G[from].push_back({cost,to});
        }
    }
    
    std::vector<long long> solve(int s){
        std::vector<long long> d;
        //INFで初期化する
        for(int i=0;i<V;i++){
            d.push_back(INF);
        }
        if(E==0){
            return d;
        }
        d[s]=0;
        que.push({0,s});
        //queは{cost,to}をコストが小さい順に出す
        while(!que.empty()){
            P p = que.top();
            que.pop();
            int v=p.second;
            if(d[v]<p.first)continue;
            for(int i=0;i<G[v].size();i++){
                P e = G[v][i];
                if(d[e.second]>d[v]+e.first){
                    d[e.second] = d[v]+e.first;
                    que.push({d[e.second],e.second});
                }
            }
        }
        return d;
    }
    
    //init:{ {int,int},longlong}のvectorを渡すことで有向グラフを構築する
    //solve:始点を渡すとinitで構築したグラフでダイクストラをし、コストのvectorを返す
};


signed main(void){
    int H,W;
    string S[57];
    cin >> H >> W;
    REP(i,H){
        cin >> S[i];
    }
    
    vector<pair<pair<int,int>,int>> edges;
    REP(i,H){
        REP(j,W){
            if(S[i][j]!='.')continue;
            if(j!=W-1 && S[i][j+1]=='.'){
                edges.push_back({{i*100+j,i*100+(j+1)},1LL});
                edges.push_back({{i*100+(j+1),i*100+j},1LL});
            }
            if(i!=H-1 && S[i+1][j]=='.'){
                edges.push_back({{i*100+j,(i+1)*100+j},1LL});
                edges.push_back({{(i+1)*100+j,i*100+j},1LL});
            }
        }
    }
    Dijkstra daiku;
    daiku.init(edges);
    vector<int> d=daiku.solve(0);
    int cost;
    if(d.size()<=(H-1)*100+(W-1)){
        cost = 1LL<<61;
    }else{
        cost=d[(H-1)*100+(W-1)];
    }
    
    
    //REP(i,H){REP(j,W){cout << d[i*100+j] << " ";}cout << endl;}
    int black=0;
    REP(i,H){
        REP(j,W){
            if(S[i][j]!='.'){
                black++;
            }
        }
    }
    if(cost==(1LL<<61)){
        cout << -1 << endl;
        return 0;
    }
    //cout << cost << endl;
    cout << H*W-(cost+1)-black << endl;
    return 0;
}



Measured memory_bytes: 1020