/* imports all std modules {{{*/
import
  std.algorithm,
  std.array,
  std.ascii,
  std.base64,
  std.bigint,
  std.bitmanip,
  std.compiler,
  std.complex,
  std.concurrency,
  std.container,
  std.conv,
  std.csv,
  std.datetime,
  std.demangle,
  std.encoding,
  std.exception,
  std.file,
  std.format,
  std.functional,
  std.getopt,
  std.json,
  std.math,
  std.mathspecial,
  std.meta,
  std.mmfile,
  std.net.curl,
  std.net.isemail,
  std.numeric,
  std.parallelism,
  std.path,
  std.process,
  std.random,
  std.range,
  std.regex,
  std.signals,
  std.socket,
  std.stdint,
  std.stdio,
  std.string,
  std.system,
  std.traits,
  std.typecons,
  std.uni,
  std.uri,
  std.utf,
  std.uuid,
  std.variant,
  std.zip,
  std.zlib;
/*}}}*/

/* libnum {{{*/
void amax(N, Args...)(ref N x, Args y) {
  x = x.max(y);
}
void amin(N, Args...)(ref N x, Args y) {
  x = x.min(y);
}
auto maxE(R)(R r) if (isInputRange!R) {
  ElementType!R x = r.front;
  r.popFront();
  foreach (y; r) x.amax(y);
  return x;
}
auto minE(R)(R r) if (isInputRange!R) {
  ElementType!R x = r.front;
  r.popFront();
  foreach (y; r) x.amin(y);
  return x;
}
/*}}}*/

/+---test
3 3
..#
#..
...
---+/
/+---test
10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................
---+/

void main(string[] args) {
  long H, W;
  readf(" %d %d ", &H, &W);

  auto M = new bool[][](H);
  foreach (ref m; M) {
    m = readln.chomp.map!(c => c == '.').array;
  }

  struct Point { long x, y, dep; }
  DList!Point q;
  q.insertBack(Point(0, 0, 1));

  auto visited = new bool[][](H, W);

  long wh = -1;
  while (!q.empty) {
    const pt = q.front;
    q.removeFront;

    if (pt.x < 0 || pt.y < 0 || pt.x >= W || pt.y >= H) continue;
    if (!M[pt.y][pt.x]) continue;
    if (visited[pt.y][pt.x]) continue;
    visited[pt.y][pt.x] = true;

    if (pt.x == W-1 && pt.y == H-1) {
      wh = pt.dep;
      break;
    }

    q.insertBack(Point(pt.x+1, pt.y, pt.dep+1));
    q.insertBack(Point(pt.x-1, pt.y, pt.dep+1));
    q.insertBack(Point(pt.x, pt.y+1, pt.dep+1));
    q.insertBack(Point(pt.x, pt.y-1, pt.dep+1));
  }

  if (wh == -1) {
    "-1".writeln;
  } else {
    (M.map!(x => x.count(true)).sum - wh).writeln;
  }
}
