import Data.Array
import Data.Array.ST
import Control.Monad.ST
import Control.Monad

--引数座標は探索済みである。
width_s::Array (Int,Int) Char ->(Int,Int) ->STArray s (Int,Int) (Maybe Int) ->(Int,Int)->ST s ()
width_s ar (b_h,b_w) tbl (h,w) = do
  Just now<-readArray tbl (h,w)
  let around = filter (\ (x,y) -> (x>=1)&&(y>=1)&&(x<=b_h)&&(y<=b_w)) [(h+1,w),(h,w+1),(h-1,w),(h,w-1)]
  let not_b  = filter (\ (x,y) -> ar!(x,y)=='.') around
  rec<-forM not_b (\ (x,y) -> do
                  dat<-readArray tbl (x,y)
                  case dat of
                    Just _  -> return Nothing
                    Nothing -> do
                      writeArray tbl (x,y) (Just (now+1))
                      return (Just (x,y)))
  forM_ rec (\ idx -> case idx of
                      Nothing   -> return ()
                      Just (x,y)->width_s ar (b_h,b_w) tbl (x,y))

--引数座標はこれから探索する。
dep_s::Array (Int,Int) Char->(Int,Int)->STArray s (Int,Int) (Maybe Int)->Int->(Int,Int)->ST s ()
dep_s ar (b_h,b_w) tbl now (h,w) = if (h>b_h) || (w>b_w) || (h<1) || (w<1)
                                          then return ()
                                          else if ar!(h,w)== '#'
                                                  then return ()
                                                  else do
                                                    dat<-readArray tbl (h,w)
                                                    case dat of
                                                      Just _   -> return ()
                                                      Nothing  -> do
                                                        writeArray tbl (h,w) (Just now)
                                                        let rec = [(h+1,w),(h,w+1),(h-1,w),(h,w-1)]
                                                        forM_ rec (dep_s ar (b_h,b_w) tbl (now+1))
                                                        return ()

init_tbl::(Int,Int)->ST s (STArray s (Int,Int) (Maybe Int))
init_tbl (h,w) = do
  ar<-newArray ((1,1),(h,w)) Nothing
  return ar


count_blanc::Array (Int,Int) Char -> Int
count_blanc ar = length (filter (\ x -> x=='.') (elems ar))


solver::Array (Int,Int) Char ->(Int,Int) -> Int
solver ar (h,w) = runST $ do
  tbl<-init_tbl (h,w)
  writeArray tbl (1,1) (Just 0)
  width_s ar (h,w) tbl (1,1)
  let b = count_blanc ar
  dat<-readArray tbl (h,w)
  case dat of
    Nothing -> return (-1)
    Just x  -> return (b - (x+1))

main::IO()
main=do
  hwc<-getLine
  let h:w:[]=map read (words hwc)
  dat<- forM [1..h] (\ _ -> getLine)
  let d = concat dat
  let ar = listArray ((1,1),(h,w)) d
  print (solver ar (h,w))
