#include <bits/stdc++.h>
using namespace std;
int main(){
    int H, W, B = 0;
    cin >> H >> W;
    vector<int> color(H * W);
    vector<vector<int>> grid(H * W);
    for (int i = 0; i < H; i++) {
        string S;
        cin >> S;
        for (int j = 0; j < W; j++) {
            color.at(i * W + j) = S.at(j);
            if (S.at(j) == '#') {
                B++;
            }
        }
    }
    for (int i = 0; i < H * W; i++) {
        if (color.at(i) == '#') {
            continue;
        }
        if (i >= W) {
            if (color.at(i - W) == '.') {
                grid.at(i).push_back(i - W);
            }
        }
        if (i < W * (H - 1)) {
            if (color.at(i + W) == '.') {
                grid.at(i).push_back(i + W);
            }
        }
        if (i % W != 0) {
            if (color.at(i - 1) == '.') {
                grid.at(i).push_back(i - 1);
            }
        }
        if (i % W != W - 1) {
            if (color.at(i + 1) == '.') {
                grid.at(i).push_back(i + 1);
            }
        }
    }
    vector<int> dist(H * W,-1);
    queue<int> que;
    que.push(0);
    dist.at(0) = 0;
    while(!que.empty()) {
        int v = que.front();
        que.pop();
        for (int i = 0; i < (int) grid.at(v).size(); i++){
            int nv = grid.at(v).at(i);
            if (dist.at(nv) != -1 && dist.at(nv) <= dist.at(v) + 1) {
                continue;
            }
            dist.at(nv) = dist.at(v) + 1;
            que.push(nv);
        }    
    }
    if (dist.at(H * W - 1) != -1) {
        cout << H * W - B - 1 - dist.at(H * W - 1) << endl;
    }
    else {
        cout << -1 << endl;
    }
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main(){
  ll h,w;
  cin >> h >> w;
  char c[h][w];
  ll dist[h][w];
  ll ans=0;
  rep(i,h){
    rep(j,w){
      cin >> c[i][j];
      dist[i][j]=-1;
      if(c[i][j]=='.'){
        ans++;
      }
    }
  }
  queue<pair<ll,ll>> que;
  que.push(make_pair(0,0));
  int x[4]={1,0,-1,0};
  int y[4]={0,1,0,-1};
  dist[0][0]=0;
  while(!que.empty()){
    auto p=que.front();
    ll cpx=p.first;
    ll cpy=p.second;
    que.pop();
    for(int k=0;k<4;k++){
      int xx=cpx+x[k];
      int yy=cpy+y[k];
      if(xx<0 || xx>=h || yy>=w || yy<0 || c[xx][yy]=='#'){
        continue;
      }
      if(dist[xx][yy]!=-1){
        continue;
      }
      dist[xx][yy]=dist[cpx][cpy]+1;
      que.push(make_pair(xx,yy));
    }
  }
  if(dist[h-1][w-1]==-1){
    cout << -1;
    return 0;
  }
  cout << ans-1-dist[h-1][w-1];
}
  