#include <cstdio>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int, int> P; typedef queue<P> Q;
int H, W, cntb, scr[52][52]; char s[52][52]; Q q;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int main() {
  scanf("%d %d", &H, &W);
  for(int i = 1; i <= H; ++i) {
    scanf("%s", &s[i][1]);
    for(int j = 1; j <= W; ++j) {
      if(s[i][j] == '#') cntb++;
    }
  }
  q.push(make_pair(1, 1));
  while(!q.empty()) {
    P p = q.front(); q.pop();
    int x = p.first; int y = p.second;
    for(int i = 0; i < 4; ++i) {
      if(scr[x+dx[i]][y+dy[i]] != 0 || s[x+dx[i]][y+dy[i]] != '.') continue;
      q.push(make_pair(x+dx[i], y+dy[i]));
      scr[x+dx[i]][y+dy[i]] = scr[x][y]+1;
    }
  }
  printf("%d\n", scr[H][W]?H*W-cntb-scr[H][W]-1:-1);
}