(* O(h w) *)
let h, w = Scanf.scanf " %d %d" @@ fun a b -> a, b
let b = ref 0
let sss = Array.init h @@ fun _ ->
  Array.init w @@ fun _ -> Scanf.scanf " %c" @@ fun c -> if c = '#' then incr b; c
let ds = Array.make_matrix h w @@ -1
let _ =
  let q = Queue.create () in
  ds.(0).(0) <- 0;
  Queue.push (0, 0) q;
  while not @@ Queue.is_empty q do
    let y0, x0 = Queue.pop q in
    let f (dy, dx) =
      let y, x = y0 + dy, x0 + dx in
      if 0 <= y && y < h && 0 <= x && x < w && ds.(y).(x) = -1 && sss.(y).(x) <> '#' then
        (ds.(y).(x) <- ds.(y0).(x0) + 1;
        Queue.push (y, x) q) in
    List.iter f [0, -1; -1, 0; 0, 1; 1, 0]
  done;
  let g = ds.(h - 1).(w - 1) in
  Printf.printf "%d\n" @@ if g = -1 then -1 else h * w - !b - g - 1