#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using ld  = long double;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
const int INF=1e9+7;
const ll LINF=1LL<<60;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld EPS = 1e-9; //微調整用（EPSより小さいと0と判定など）

//MODINT
//変数名.nでint型の数値を受け取る
struct mint {
  int n;
  mint(int n_ = 0) : n(n_) {}
};
 
mint operator+(mint a, mint b) { a.n += b.n; if (a.n >= MOD) a.n -= MOD; return a; }
mint operator-(mint a, mint b) { a.n -= b.n; if (a.n < 0) a.n += MOD; return a; }
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }

int ii() { int x; scanf("%d", &x); return x; }
long long il() { long long x; scanf("%lld", &x); return x; }
string is() { string x; cin >> x; return x; }
char ic() { char x; cin >> x; return x; }
void oi(int x) { printf("%d ", x); }
void ol(long long x) { printf("%lld ", x); }
void od(double x) { printf("%.15f ", x); }
void os(const string &s) { printf("%s ", s.c_str()); }
void oc(const char &c) { printf("%c ", c); }
auto op = [&](auto p) -> void{ cout << p; };
auto ov = [&](auto vec) -> void{ cout << vec; };
void br() { putchar('\n'); }
 
#define gcd __gcd //llは受け取ってくれない
int lcm(int a, int b){return a / gcd(a, b) * b;}
#define ALL(a) a.begin(),a.end() //sort(ALL(vec));
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define PB push_back
#define SZ(x) ((int)(x).size) //size()がunsignedなのでエラー避けに

//4近傍（上下左右）
vector<int> dx_4 = {1, 0, -1, 0};
vector<int> dy_4 = {0, 1, 0, -1};

// coutによるpairの出力（空白区切り）
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << " " << p.second << ")";}
// 空白区切りだけ --> return s << "(" << p.first << " " << p.second << ")";
// 見やすくしたいとき --> return s << "(" << p.first << " " << p.second << ")";
// coutによるvectorの出力（空白区切り）
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
	int len = v.size();
	for (int i = 0; i < len; ++i) {
		s << v[i]; if (i < len - 1) s << " "; //"\t"に変えるとTabで見やすく区切る
	}
	return s;
}
// coutによる多次元vectorの出力（空白区切り）
template<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& vv) {
	int len = vv.size();
	for (int i = 0; i < len; ++i) {
		s << vv[i] << endl;
	}
	return s;
}

//最大値、最小値を更新する。aよりbのが大きい（小さい）か等しければaを更新してtrueを返す。そうでなければ何もせずfalseを返す chmax(nowmax,x);
template<typename T>
bool chmax(T& a, T b){return (a = max(a, b)) == b;}
template<typename T>
bool chmin(T& a, T b){return (a = min(a, b)) == b;}

// ------------ template - //


// - global -------------- //
 

// -------------- global - //


// - library ------------- //



// ------------- library - //


int main() {

  ll H = il();
  ll W = il();

  vector<vector<char> > grids(H, vector<char>(W));
  ll bnum = 0;

  rep(h, H){
    rep(w, W){
      char tmp = ic();
      grids.at(h).at(w) = tmp;
      if (tmp == '#' ){
        bnum += 1;
      }
    }
  }

  //start, goalを設定
  ll sh = 0;
  ll sw = 0;
  ll gh = H-1;
  ll gw = W-1;

  queue<pll> q;
//  vector<vector<bool> > seen(H, vector<bool>(W, false));
  vector<vector<ll> > dist(H, vector<ll>(W, -1)); //-1なら未訪問
  vector<vector<pll > > prev(H, vector<pll>(W, MP(-1LL, -1LL) ));
  
  auto inRange = [&](ll h, ll w) -> bool {
    return 0 <= h && 0 <= w && h < H && w < W;
  };
  
  //(sh, sw)から最短経路をBFS
  dist[sh][sw] = 0;
  q.emplace(sh, sw);

  while (!q.empty()){
    ll nh = q.front().first;
    ll nw = q.front().second;
    q.pop();
    
    // 次の候補を探索
    rep(i, 4){
      ll nexh = nh + dy_4[i];
      ll nexw = nw + dx_4[i];
      if (inRange(nexh, nexw)){
        if (dist[nexh][nexw]==-1 && grids[nexh][nexw] == '.' ){ 
          prev[nexh][nexw] = MP(nh, nw);
          dist[nexh][nexw] = dist[nh][nw] + 1;
          q.emplace(nexh, nexw);
        }
      }
    }
  }
  //探索終了
  ol(dist[gh][gw] == -1 ? -1 : H*W - bnum - 1 - dist[gh][gw]);
  
  
// -- main() end -- //
}


Measured memory_bytes: 256