fun main(args : Array<String>) {
  val (h, w) = readLine()!!.split(" ").map { it.toInt() }
  val dp = Array(h) { IntArray(w) { 0 } }.apply { get(0)[0] = 1 }
  (0 until h).forEach { x ->
    readLine()!!.forEachIndexed { y, s -> if (s == '#') dp[x][y] = -1 }
  }
  set(dp, 0, 0)
  print(if (dp[h - 1][w - 1] == 0) -1 else h * w - dp[h - 1][w - 1] - dp.map { it.count { it == -1 } }.sum())
}
fun set(dp: Array<IntArray>, x: Int, y: Int) {
  if (y > 0 && dp[x][y - 1] != -1 && (dp[x][y - 1] == 0 || dp[x][y - 1] > dp[x][y] + 1)) {
    dp[x][y - 1] = dp[x][y] + 1
    set(dp, x, y - 1)
  }
  if (x > 0 && dp[x - 1][y] != -1 && (dp[x - 1][y] == 0 || dp[x - 1][y] > dp[x][y] + 1)) {
    dp[x - 1][y] = dp[x][y] + 1
    set(dp, x - 1, y)
  }
  if (x < dp.size - 1 && dp[x + 1][y] != -1 && (dp[x + 1][y] == 0 || dp[x + 1][y] > dp[x][y] + 1)) {
    dp[x + 1][y] = dp[x][y] + 1
    set(dp, x + 1, y)
  }
  if (y < dp[0].size - 1 && dp[x][y + 1] != -1 && (dp[x][y + 1] == 0 || dp[x][y + 1] > dp[x][y] + 1)) {
    dp[x][y + 1] = dp[x][y] + 1
    set(dp, x, y + 1)
  }
}