void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.exception;

  int h, w; rd(h, w);
  auto c=new char[][](h, w);
  foreach(i; 0..h) c[i]=readln.chomp.to!(char[]);

  auto dir=[[1, 0], [-1, 0], [0, 1], [0, -1]],
       vis=new bool[][](h, w);
  void dfs(int i, int j){
    vis[i][j]=true;
    foreach(d; dir){
      auto ni=i+d[0], nj=j+d[1];
      if(ni<0 || ni>=h || nj<0 || nj>=w) continue;
      if(vis[ni][nj]) continue;
      if(c[ni][nj]=='.') dfs(ni, nj);
    }
  }
  
  dfs(0, 0);
  if(vis[h-1][w-1]==false){writeln(-1); return;}

  int bk=0;
  foreach(i; 0..h) bk+=count(c[i], '#');
  auto res=h*w-bk-(h+w-1);
  enforce(res>=0);
  writeln(h*w-bk-(h+w-1));
}

/+
  370-115-46=209
+/

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}