
/*
g++ --std=c++17 A.cpp
*/

#include <iostream>
#include <iomanip> //! for setprecision(20)
#include <math.h>
#include <algorithm>
#include <functional> 
#include <string>
#include <vector>

#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <limits.h>

using namespace std;

typedef long long LL;

#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define dump(c) { for (auto it = c.begin(); it != c.end(); ++it) if (it == c.begin()) cout << *it; else cout << ' ' << *it; cout << endl; } 
#define dumpMap(m) { for (auto it: m) cout << it.first << "=>" << it.second << ' '; }

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

const int MOD = 1000000007;


int dist[50][50];
string s[50];



int main()
{
    const int INF = 1e9;
	cin.tie(0);
	ios::sync_with_stdio(0);

    int h, w;
    cin >> h >> w;
    rep(i, h) cin >> s[i];
    int whiteCount = 0;
    rep(y, h) rep(x, w) {
        dist[y][x] = INF;
        if (s[y][x] == '.') ++whiteCount;
    }
    dist[0][0] = 1;
    queue<pair<int, int>> q;
    q.push({0, 0});
    while (!q.empty()) {
        int y = q.front().first; int x = q.front().second; q.pop();
        int d = dist[y][x] + 1;
        rep(i, 4) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' && d < dist[ny][nx]) {
                dist[ny][nx] = d;
                q.push({ny, nx});
            }
        }
    }

    int ans = -1;
    if (dist[h-1][w-1] != INF) {
        ans = whiteCount - dist[h-1][w-1];
    }
    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll MOD = 1000000007;

int main(){
  int H,W;cin >> H >> W;
  vector<string> S(H);
  int cnt=0;
  for(int i=0;i<H;i++){
    cin >> S[i];
    for(int j=0;j<W;j++){
      if(S[i][j]=='#'){
        cnt ++;
      }
    }
  }
  vector<vector<bool>> P(H,vector<bool>(W,false));
  queue<int> X;
  queue<int> Y;
  queue<int> D;
  X.push(0);
  Y.push(0);
  D.push(1);
  P[0][0] = true;
  int goalD=0;
  while(!X.empty()){
    int x = X.front(),y=Y.front(),d=D.front();
    X.pop();Y.pop();D.pop();
    if(x==H-1&&y==W-1){
      goalD = d;
      break;
    }
    if(x-1>=0){
      if(S[x-1][y]=='.'&&!P[x-1][y]){
        X.push(x-1);Y.push(y);D.push(d+1);
        P[x-1][y] = true;
      }
    }
    if(x+1<H){
      if(S[x+1][y]=='.'&&!P[x+1][y]){
        X.push(x+1);Y.push(y);D.push(d+1);
        P[x+1][y] = true;
      }
    }
    if(y-1>=0){
      if(S[x][y-1]=='.'&&!P[x][y-1]){
        X.push(x);Y.push(y-1);D.push(d+1);
        P[x][y-1] = true;
      }
    }
    if(y+1<W){
      if(S[x][y+1]=='.'&&!P[x][y+1]){
        X.push(x);Y.push(y+1);D.push(d+1);
        P[x][y+1] = true;
      }
    }
  }
  if(P[H-1][W-1]){
    cout << H*W - cnt - goalD << endl;
  }else{
    cout << -1 << endl;
  }
}