import java.io.{BufferedReader, InputStream, InputStreamReader}
import java.util.StringTokenizer

import scala.collection.mutable
import scala.util.Sorting
import math.{abs, max, min}
import scala.collection.mutable.ArrayBuffer

object Main {
  val MOD = 1000000007
  def solve(): Unit = {
    val sc = new InputReader(System.in)
    val H = sc.nextInt()
    val W = sc.nextInt()

    val s = new Array[Array[Char]](H)
    for {
      h <- 0 until H
    } s(h) = sc.next.toCharArray

    val INF = H * W
    val d = Array.fill[Int](H, W)(INF)
    val pre = Array.ofDim[Coordinate](H, W)
    val edges = Seq((1, 0), (0, 1), (-1, 0), (0, -1))
    type Coordinate = (Int, Int)
    type Elm = (Int, Coordinate, Coordinate)
    val que = mutable.Queue.empty[Elm]
    visit((0, 0), 0)
    d(0)(0) = 0

    def visit(p: Coordinate, cost: Int): Unit = {
      edges foreach { e =>
        val h = p._1 + e._1
        val w = p._2 + e._2
        if (h >= 0 && h < H && w >= 0 && w < W && s(h)(w) == '.') {
          que.enqueue((cost + 1, p, (h, w)))
        }
      }
    }

    while(que.nonEmpty) {
      val (cost, p0, p) = que.dequeue()
      if (d(p._1)(p._2) > cost) {
        d(p._1)(p._2) = cost
        pre(p._1)(p._2) = p0
        visit(p, cost)
      }
    }

    if (d(H - 1)(W - 1) == INF) println(-1)
    else {
      val minPath = Array.ofDim[Boolean](H, W)
      def step(p: Coordinate): Unit = {
        minPath(p._1)(p._2) = true
        p match {
          case (0, 0) =>
          case _ => step(pre(p._1)(p._2))
        }
      }
      step((H - 1, W - 1))

      val ans = (for {
        h <- 0 until H
        w <- 0 until W
      } yield s(h)(w) == '.' && !minPath(h)(w)).count(identity)

      println(ans)
    }
  }


  def main(args: Array[String]): Unit = {
    solve()
  }


  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = null

    def next: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next.toInt
    def nextLong(): Long = next.toLong
    def nextChar(): Char = next.charAt(0)
  }
}
