import scala.collection.mutable.Queue

object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val h, w = sc.nextInt()
  val g = Array.fill(h)(sc.next().map(_ == '#').toArray)
  val walls = g.map(_.count(identity)).sum
  val sp = bfs(0, 0, h, w, g)(h-1)(w-1)
  println(if (sp == -1) -1 else (w * h) - walls - sp - 1)

  def bfs(sx: Int, sy: Int, w: Int, h: Int, g: Array[Array[Boolean]]): Array[Array[Int]] = {
    val ans = Array.fill(w, h)(-1)
    val queue = Queue((sx, sy))
    ans(sx)(sy) = 0
    queue.enqueue((sx, sy))

    val d = List((1,0),(-1,0),(0,1),(0,-1))
    while (queue.nonEmpty) {
      val (x1, y1) = queue.dequeue
      for ((dx, dy) <- d) {
        val x2 = x1 + dx
        val y2 = y1 + dy
        if (0 <= x2 && x2 < w && 0 <= y2 && y2 < h && !g(x2)(y2) && ans(x2)(y2) == -1) {
          ans(x2)(y2) = ans(x1)(y1) + 1
          queue.enqueue((x2, y2))
        }
      }
    }
    ans
  }
}
