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