// g++ macro.cpp -std=c++14
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;
using namespace std;

#define dump(x)  cout << #x << " = " << (x) << endl;
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) << endl

#define SANKOU(n,a,b) cout << ((n) ? (#a) : (#b) ) << endl

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)

#define FOREACH(x,a) for(auto& (x) : (a) )

#define WFA(d,v) REP(k,v)REP(i,v)REP(j,v)d[i][j]=min(d[i][j],d[i][k]+d[k][j])

#define SCOUT(x) cout<<(x)<<" "
#define ENDL cout<<endl

#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define VECIN2(x,y) REP(i,x.size())cin>>x[i]>>y[i]
#define VECCOUT(x) for(auto&youso_: (x) )cout<<youso_<<" ";cout<<endl

#define ALL(obj) (obj).begin(),(obj).end()

#define EXIST(n,x) (find(ALL(n),x)!=n.end())
#define UNIQUE(obj) sort(ALL( obj )); obj.erase(unique(ALL(obj)),obj.end())
#define COUT(x) cout<<(x)<<endl
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
    cin>>head;
    CINT(move(tail)...);
}
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)

//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision; // cpp_int

#define P pair<int,int>
#define V vector<int>
#define M map<int,int>
#define S set<int>
#define L list<int>

#define pb(a) push_back(a)
#define mp make_pair

class Dijkstra{
public:
    Dijkstra(vector<M> ed,int firs,int pnum);
    void Solve();
    V range;// range[a]=b;
    vector<M> edges;// [a][b]=c;
    priority_queue<P,vector<P>,greater<P>> que;// queue=(range,point);
    V keiro;
    V CalcRoute(int startp,int endp);
};

Dijkstra::Dijkstra(vector<M> ed,int firs,int pnum){
    edges=ed;
    V ttt(pnum,INF);
    range=ttt;
    range[firs]=0;
    que.push(mp(0,firs));
    keiro=V(pnum);
    REP(i,pnum)keiro[i]=i;
}

void Dijkstra::Solve(){
    while(!que.empty()){
        P top_=que.top();
        que.pop();
        FOREACH(x,edges[top_.second]){
            if(range[x.first] > range[top_.second] + x.second){
                range[x.first] = range[top_.second] + x.second;
                que.push(mp(range[x.first],x.first));
                keiro[x.first] = top_.second;
            }
        }
    }
}
V Dijkstra::CalcRoute(int startp,int endp){
  V vec(1,endp);
  int point = endp;
  while(point==startp){
    point = keiro[point];
    vec.pb(point);
  }
  return vec;
}

int main(){
  CIN(h,w);vector<V> vec(h,V(w,0));
  REP(i,h){
    SCIN(str);
    REP(j,w)vec[i][j]=(str[j]=='#'); // 1なら壁
  }
  ll tsurosum=0;//白の数
  REP(i,h)REP(j,w)if(vec[i][j]==0)tsurosum++;
 
  vector<M> edges(h*w);

  REP(i,h)REP(j,w){
    if(vec[i][j]==0){
      if(i!=0&&vec[i-1][j]==0){edges[i*w+j][i*w+j-w]=1;edges[i*w+j-w][i*w+j]=1;}
      if(i!=h-1&&vec[i+1][j]==0){edges[i*w+j][i*w+j+w]=1;edges[i*w+j+w][i*w+j]=1;}
      if(j!=w-1&&vec[i][j+1]==0){edges[i*w+j][i*w+j+1]=1;edges[i*w+j+1][i*w+j]=1;}
      if(j!=0&&vec[i][j-1]==0){edges[i*w+j-1][i*w+j]=1;edges[i*w+j][w*i+j-1]=1;}
    }
  }
  Dijkstra dijk(edges,0,h*w);
  dijk.Solve();
  if(dijk.range[h*w-1]==INF){COUT(-1);return 0;}
  COUT(tsurosum - dijk.range[h*w-1] - 1);

  return 0;
}



Measured memory_bytes: 2048