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

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

  alias pair=Tuple!(int, int);
  auto d=new int[][](h, w);
  const int inf=1_000_000_000;
  foreach(i; 0..h) fill(d[i], inf);
  auto Q=new DList!(pair);
  d[0][0]=0; Q.insertBack(pair(0, 0));
  while(Q.empty==false){
    auto cur=Q.front; Q.removeFront;
    auto i=cur[0], j=cur[1];
    foreach(dd; [[1, 0], [-1, 0], [0, 1], [0, -1]]){
      auto ni=i+dd[0], nj=j+dd[1];
      if(ni<0 || ni>=h || nj<0 || nj>=w) continue;
      if(c[ni][nj]=='#') continue;
      if(d[i][j]+1<d[ni][nj]){
        d[ni][nj]=d[i][j]+1;
        Q.insertBack(pair(ni, nj));
      }
    }
  }

  if(d[h-1][w-1]==inf){writeln(-1); return;}
  int bk=0;
  foreach(i; 0..h) bk+=count(c[i], '#');
  auto res=h*w-bk-(d[h-1][w-1]+1);
  writeln(res);
}
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));
  }
}