import scala.io.{StdIn => in}

object Main extends App {
  val Array(h, w) = in.readLine.split(" ").map(_.toInt)
  val maze = Iterator.continually(in.readLine)
    .take(h)
    .map(_.map(ch => if(ch == '.') true else false).toArray)
    .toArray

  val white = maze.map(_.count(_ == true)).sum
  
  val res = {
    val Array(sy, sx, gy, gx) = Array(0, 0, h-1, w-1)
    val dx = Array( 0, 1, 0,-1)
    val dy = Array( 1, 0,-1, 0)
    val map = Array.ofDim[Int](h,w)
    val que = collection.mutable.Queue.empty[(Int,Int)]
    val s = (sy,sx)
    que += s
    maze(sy)(sx) = false

    while(!que.isEmpty) {
      val(y,x) = que.dequeue

      if(y == gy && x == gx) que.clear
      else {
        for(i <- 0 until 4) {
          val nx = x + dx(i)
          val ny = y + dy(i)
          if(0 <= nx && nx < w && 0 <= ny && ny < h && maze(ny)(nx)) {
            map(ny)(nx) = map(y)(x) + 1
            maze(ny)(nx) = false
            val next = (ny, nx)
            que += next
          }
        }
      }
    }
    // map.foreach(e => println(e.mkString))
    map(gy)(gx)
  }

  val ans = if(res == 0) -1 else white - res -1
  println(ans)
}