#include <bits/stdc++.h>
#include <set>
using namespace std;
using pii = pair<int,int>;
using pll = pair<long long, long long>;
const long long INF = 1<<29;

const int MOD = 998244353;
long long ruizyou(long long m,long long n){
  if(m == 0)return 0;
  if(m == 1)return 1;
  long long ans = 1;
  long long tmp = m;
  for(int i=0;i<=30;i++){
    if(n & (1<<i)){
      ans *= tmp;
      ans %= MOD;
    }
    tmp *= tmp;
    tmp %= MOD;
  }
  return ans;
}
long long kaizyou(long long x){
  if(x == 0)return 1;
  return x * kaizyou(x-1) % MOD;
}
long long comb(long long x,long long y){
  long long bunsi = kaizyou(x);
  long long bunbo = kaizyou(x-y) * kaizyou(y) % MOD;
  return bunsi * ruizyou(bunbo,MOD-2) % MOD;
}
struct unionfind{
  vector<int> par;
  vector<vector<int>> children;
  vector<int> hukasa;
  vector<int> kazu;
  unionfind(int n){
    par = vector<int>(n);
    hukasa = vector<int>(n,0);
    kazu = vector<int>(n,1);
    for(int i=0;i<n;i++){
      par.at(i) = i;
    }
  }
  int root(int x){
    if(par.at(x) == x)return x;
    return root(par.at(x));
  }
  void unite(int x,int y){
    int rx = root(x);
    int ry = root(y);
    if(rx == ry)return;
    if(hukasa.at(rx) >= hukasa.at(ry)){
      par.at(ry) = rx;
      hukasa.at(rx) = max(hukasa.at(ry) + 1,hukasa.at(rx));
      kazu.at(rx) += kazu.at(ry);
    }
    else{
      par.at(rx) = ry;
      kazu.at(ry) += kazu.at(rx);
    }
  }
  void newkazu(){
    int n = kazu.size();
    for(int i=0;i<n;i++){
      kazu.at(i) = kazu.at(root(i));
    }
  }
  bool same(int x,int y){
    return root(x) == root(y);
  }
};
int ctoi(char a){
  return a - '0';
}
long long gcd(long long a,long long b){
  long long c = max(a,b);
  long long d = min(a,b);
  if(d % c == 0)return c;
  return gcd(c,d%c);
}
long long lcm(long long a,long long b){
  return a * b / gcd(a,b);
}
int main(){
  int h,w;
  cin >> h >> w;
  vector<vector<char>> s(h,vector<char>(w));
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++)cin >> s.at(i).at(j);
  }
  vector<vector<int>> table(h,vector<int>(w,-1));
  queue<pii> q;
  q.push(pii(0,0));
  table.at(0).at(0) = 1;
  bool flag = false;
  int count;
  while(!q.empty()){
    pii tmppair = q.front();
    q.pop();
    int x,y;
    tie(x,y) = tmppair;
    set<pii> se;
    if(x-1 >= 0)se.insert(pii(x-1,y));
    if(x+1 < h)se.insert(pii(x+1,y));
    if(y-1 >= 0)se.insert(pii(x,y-1));
    if(y+1 < w)se.insert(pii(x,y+1));
    for(auto t:se){
      int tx,ty;
      tie(tx,ty) = t;
      if(s.at(tx).at(ty) == '.' && table.at(tx).at(ty) == -1){
        table.at(tx).at(ty) = table.at(x).at(y) + 1;
        q.push(t);
      }
      if(t == pii(h-1,w-1)){
        count = table.at(tx).at(ty);
        flag = true;
        break;
      }
    }
    if(flag) break;
  }
  if(!flag) cout << -1 << endl;
  else{
    int count2 = 0;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(s.at(i).at(j) == '#')count2++;
      }
    }
    cout << h * w - count - count2 << endl;
  }
}
#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL<<60;
const long long int Mod = 1000000007;
using ll = long long int; using ci = const int;
using vi = vector<int>;  using Vi = vector<long long int>;
using P = pair<int, int>;  using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define rp(i,N) for(ll i = 0; i < (ll)N; i++)
#define repi(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
#define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v> >
template<class T> inline void princ(T x){cout<<x<<" ";}; 
template<class T> inline void print(T x){cout<<x<<"\n";};
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
///////////////////////////////////////////////////////////////////////////////////


void solve(){
    matrix dxdy={{1,0},{-1,0},{0,1},{0,-1}};
    ll h,w;
    cin >> h >> w;
    vector<vector<char>> s(h,vector<char>(w));
    ll ctr=0;
    rp(i,h){
        rp(j,w){
            cin >> s[i][j];
            if(s[i][j]=='#') ctr++;
        }
    }
    queue<pair<P,ll>> que;
    que.push(mp(mp(0,0),0));
    matrix seen(h,Vi(w,0));
    ll shortest=-1;
    while(!que.empty()){
        P loc=que.front().first;
        ll dist=que.front().second;
        que.pop();
        if(seen[loc.first][loc.second]==1) continue;
        if(loc.first==h-1&&loc.second==w-1) {
            shortest=dist;
            break;
        }
        seen[loc.first][loc.second]=1;
        for(auto vec:dxdy){
            if(loc.first+vec[0]<0||loc.first+vec[0]>=h||loc.second+vec[1]<0||loc.second+vec[1]>=w) continue;
            if(s[loc.first+vec[0]][loc.second+vec[1]]=='.'){
                que.push(mp(mp(loc.first+vec[0],loc.second+vec[1]),dist+1));
            }
        }
    }
    if(shortest==-1){
        print(-1);
    }else{
        print(h*w-ctr-shortest-1);
    }

    return;
}
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    std::cout<<std::fixed<<std::setprecision(30);
    solve();
    return 0;
}