// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<vector>

#include<functional>

#include<string>

#include<cmath>

#include<map>

#include<set>

#include<queue>

#include<stack>

#include<cstdio>

using namespace std;

using ll = long long;

using Graph = vector<vector<int>>;



int main() {

  int H, W;

  cin >> H >> W;

  vector<vector<int>> s(H * W, vector<int>(H * W, 0)), dp(H * W,vector<int>(H * W, 1001001001));

  int black = 0;

  for(int h = 0; h < H; h++) {

    string tmp;

    cin >> tmp;

    for(int w = 0; w < W; w++){

      if(tmp[w] == '.'){

        s[h][w] = 1;

      }else{

        black++;

      }

    }

  }

  if(s[0][0] == 0){

    cout << -1 << endl;

    return 0;

  }



  vector<vector<int>> dist(H, vector<int>(W, -1));

  queue<pair<int, int>> que;

  dist[0][0] = 0;

  que.push({0,0});

  int dh[4] = {1, 0, -1, 0};

  int dw[4] = {0, 1, 0, -1};

  while(!que.empty()) {

    int qh = que.front().first;

    int qw = que.front().second;

    que.pop();

    for(int i = 0; i < 4; i++){

      int nh = qh + dh[i];

      int nw = qw + dw[i];

      if(nh < 0 || nh >= H || nw < 0 || nw >= W){

        continue;

      }

      if(dist[nh][nw] != -1){

        continue;

      }

      if(s[nh][nw] == 0){

        continue;

      }

      que.push({nh, nw});

      dist[nh][nw] = dist[qh][qw] + 1;

    }

  }

  /*

  for(int i = 0; i < H * W; i++){

    cout << i << ":" << dist[i] << " ";

  }

  cout << endl;

  cout << dist[H * W - 1] << endl;

  */

  if(dist[H - 1][W - 1] == -1){

    cout << -1 << endl;

    return 0;

  }

  cout << H * W - (dist[H - 1][W - 1] + 1) - black << endl;

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.6570509514059086x):
#include<iostream>

#include<algorithm>

#include<vector>

#include<functional>

#include<string>

#include<cmath>

#include<map>

#include<set>

#include<queue>

#include<stack>

#include<cstdio>

#include<numeric>

using namespace std;

using ll = long long;

int main(){

  int h, w;

  cin >> h >> w;

  vector<vector<int>> s(h, vector<int>(w, 0));

  int black = 0;

  for(int i = 0; i < h; i++) {

    for(int j = 0; j < w; j++){

      char c;

      cin >> c;

      if(c == '.'){

        s[i][j] = 1;

      }else{

        black++;

      }

    }

  }

  vector<vector<int>> dist(h, vector<int>(w, 1001001001));

  dist[0][0] = 0;

  queue<pair<int, int>> que;

  que.push({0, 0});

  int dx[4] = {1, 0, -1, 0};

  int dy[4] = {0, 1, 0, -1};

  while(!que.empty()) {

    int x = que.front().first;

    int y = que.front().second;

    que.pop();

    for(int i = 0; i < 4; i++){

      int nx = x + dx[i];

      int ny = y + dy[i];

      if(nx < 0 || nx >= h || ny < 0 || ny >= w){

        continue;

      }

      if(dist[nx][ny] != 1001001001){

        continue;

      }

      if(s[nx][ny] == 0){

        continue;

      }

      dist[nx][ny] = dist[x][y] + 1;

      que.push({nx, ny});

    }

  }

  /*

  cout << dist[h - 1][w - 1] << endl;

  cout << black << " ";

  */

  if(dist[h - 1][w - 1] == 1001001001){

    cout << -1 << endl;

    return 0;

  }

  cout << h * w - (dist[h - 1][w - 1] + 1) - black << endl;

  return 0;

}