import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}
import scala.collection.mutable.Queue

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val H, W = in.next().toInt

  val ar = new Array[String](H+1)

  for(y <- 1 to H) {
    ar(y) = "#" + in.next()
  }

  def isCorrect(x: Int, y: Int): Boolean =
    1 <= x && x <= W && 1 <= y && y <= H

  def mk4dir(x: Int, y: Int): List[(Int, Int)] = {
    List((x+1, y), (x-1, y), (x, y+1), (x, y-1))
      .filter{ case (x, y) => isCorrect(x, y) }
  }

  //
  val q = new Queue[((Int, Int), Int)]()
  q.enqueue(((1, 1), 0))

  val dp = Array.ofDim[Int](H+1, W+1)
  for(y <- 1 to H; x <- 1 to W)
    dp(y)(x) = -1

  var hoge = -1
  while(!q.isEmpty) {
    val ((x, y), cost) = q.dequeue
    

    if((x, y) == (W, H)) {
      hoge = cost
      q.clear
    }
    else if(dp(y)(x) == -1 && ar(y)(x) != '#') {
      dp(y)(x) = cost
      mk4dir(x, y).foreach{ case (x, y) => q.enqueue(((x, y), cost+1)) }
    }
  }

  var cnt = 0
  for(y <- 1 to H; x <- 1 to W) {
    if(ar(y)(x) == '.'){
      cnt += 1
    }
  }



  if(hoge == -1)
    println(-1)
  else{
    println(cnt-1-hoge)
  }

}



class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
