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[]);

  auto n=h*w, d=new int[][](n, n);
  const int inf=1e9.to!int;
  foreach(i; 0..n) fill(d[i], inf);
  foreach(i; 0..n) d[i][i]=0;
  foreach(i; 0..h)foreach(j; 0..w){
    if(c[i][j]=='#') continue;
    foreach(dir; [[1, 0], [-1, 0], [0, 1], [0, -1]]){
      auto ni=i+dir[0], nj=j+dir[1];
      if(ni<0 || ni>=h || nj<0 || nj>=w) continue;
      if(c[ni][nj]=='#') continue;
      d[i*w+j][ni*w+nj]=d[ni*w+nj][i*w+j]=1;
    }
  }
  foreach(_; 0..n)foreach(i; 0..n)foreach(j; 0..n){
    if(d[i][j]>d[i][_]+d[_][j]) d[i][j]=d[i][_]+d[_][j];
  }
  if(d[0][n-1]==inf){writeln(-1); return;}

  int bk=0;
  foreach(i; 0..h) bk+=count(c[i], '#');
  auto res=n-bk-(d[0][n-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));
  }
}