#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> s(H, vector<char>(W));
  int memo = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> s.at(i).at(j);
      if (s.at(i).at(j) == '.') {
        memo++;
      }
    }
  }
  vector<vector<int>> M(H, vector<int>(W, 0));
  queue<int> X;
  queue<int> Y;
  X.push(0);
  Y.push(0);
  vector<int> vx = {1, -1, 0, 0}, vy = {0, 0, 1, -1};
  s.at(0).at(0) = '#';
  while (X.size() > 0) {
    for (int i = 0; i < 4; i++) {
      if ((X.front() + vx.at(i)) >= 0 && (Y.front() + vy.at(i)) >= 0 
          && (X.front() + vx.at(i)) < H && (Y.front() + vy.at(i)) < W
          && s.at(X.front() + vx.at(i)).at(Y.front() + vy.at(i)) == '.' 
          && M.at(X.front() + vx.at(i)).at(Y.front() + vy.at(i)) == 0) {
            X.push(X.front() + vx.at(i));
            Y.push(Y.front() + vy.at(i));
            M.at(X.front() + vx.at(i)).at(Y.front() + vy.at(i)) = M.at(X.front()).at(Y.front()) + 1;
      }
    }
    X.pop();
    Y.pop();
  }
  if (M.at(H - 1).at(W - 1) != 0) {
    cout << memo - (M.at(H - 1).at(W - 1) + 1) << endl;
  }
  else {
    cout << -1 << endl;
  }
}
#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

const int INF=1001001001;
int dy[4]={-1,0,1,0};
int dx[4]={0,-1,0,1};

int main(){
    int h,w;
    cin>>h>>w;
    int ans=0;
    vector<string>s(h);
    rep(i,h)cin>>s[i];
    rep(i,h)rep(j,w)if(s[i][j]=='.')ans++;
    vector<vector<int>>dist(h,vector<int>(w,INF));
    queue<P>q;
    q.push(P(0,0));
    dist[0][0]=1;
    while(!q.empty()){
        int y=q.front().first;
        int x=q.front().second;
        q.pop();
        rep(dir,4){
            int vy=y+dy[dir];
            int vx=x+dx[dir];
            if(vy<0||vy>=h||vx<0||vx>=w)continue;
            if(s[vy][vx]=='#'||dist[vy][vx]!=INF)continue;
            dist[vy][vx]=dist[y][x]+1;
            q.push(P(vy,vx));
        }
    }
    ans-=dist[h-1][w-1];
    if(dist[h-1][w-1]!=INF)cout<<ans<<endl;
    else cout<<-1<<endl;
}