(* Examples *)
(*
stdin.ReadLine()
printfn "%d"
printfn "%s"
*)

let internal INF = 1 <<< 31

(* Library *)
module Library = 
  (* Queue *)
  type Queue<'T> (capacity : int) =
    let data = Array.create (capacity + 1) Unchecked.defaultof<'T>
    let length = capacity + 1
    let mutable head = 0
    let mutable tail = 0

    member internal this.Add x = (x + 1) % length
    member internal this.Delete x = (x - 1) % length

    member this.isEmpty =
      if head = tail then true else false

    member this.isFull =
      if this.Add tail = head then true else false

    member this.Enqueue x =
      if this.isFull then 
        raise(new System.Exception("Capacity over"))
      else
        data.[tail] <- x
        tail <- this.Add tail
        ()
        
    member this.Peek =
      if this.isEmpty then 
        None
      else
        Some data.[head]

    member this.Dequeue =
      if this.isEmpty then 
        None
      else
        let result = Some data.[head]
        head <- this.Add head
        result

    member this.Clear =
      head <- 0
      tail <- 0
      ()

(* Standard Input *)
let readSplit elementFun=
  stdin.ReadLine().Split()
  |> Array.map elementFun

let read elementFun =
  stdin.ReadLine() |> elementFun

let readMap H =
  [|for i in 1..H -> stdin.ReadLine()|]
  |> Array.map(fun line ->
    line.ToCharArray()
  )
  |> array2D

(* Main *)
open Library

let [|H; W|] = readSplit int

let map = readMap H

let Q = Queue<int * int * int>(10000)


let mutable firstWhite = 0
map
|> Array2D.iter(fun x ->
  if x = '.' then firstWhite <- firstWhite + 1
)

let countMap = Array2D.create H W -1

let rec solve () =
  match Q.Dequeue with
  | Some (x, y, c) ->
    if x < 0 || x >= H || y < 0 || y >= W || map.[x, y] = '#' || countMap.[x, y] <> -1 then 
      solve ()
    else if x = H - 1 && y = W - 1 then
      countMap.[x, y] <- c
    else
      countMap.[x, y] <- c
      Q.Enqueue (x + 1, y, c + 1)
      Q.Enqueue (x, y + 1, c + 1)
      Q.Enqueue (x - 1, y, c + 1)
      Q.Enqueue (x, y - 1, c + 1)
      solve ()
  | _ -> ()

Q.Enqueue (0, 0, 1)
solve ()

let result = countMap.[H - 1, W - 1]

(
if result = -1 then
  -1
else
  firstWhite - result
)
|> printfn "%d"