import Control.Monad
import Control.Monad.ST
import Data.Array.Unboxed
import Data.Array.ST
import Data.Maybe

main = do
    (h:w:_)<-map read.words<$>getLine::IO [Int]
    m <- concat <$> replicateM h getLine :: IO String
    --
    let wn = length $ filter(=='.') m
    let bd = listArray ((1,1),(h,w)) $ map (\c->if c=='.' then 0 else (-1)) m::UArray (Int,Int) Int
    --
    let bd' = sol' h w bd

    print $ if bd' ! (h,w) == 0 then (-1) else wn - (bd' ! (h,w))
--
sol' :: Int -> Int -> UArray (Int,Int) Int -> UArray (Int,Int) Int
sol' h w bd = runST $ do
    m <- thaw bd :: ST s (STUArray s (Int,Int) Int)
    writeArray m (1,1) 1
    solve h w m 1 [(1,1)]
    freeze m

solve :: Int -> Int -> STUArray s (Int,Int) Int -> Int -> [(Int,Int)] -> ST s ()
solve _ _ _ _ [] = return ()
solve h w m s cd = do
    nn <- forM nt $ \(i,j) ->
        if i<1 || i>h || j<1 || j>w then return Nothing
        else do
            f <- readArray m (i,j)
            when (f==0) $ writeArray m (i,j) (s+1)
            return $ if f == 0 then Just (i,j) else Nothing
    solve h w m (s+1) $ catMaybes nn
    where
        nt = concatMap mv cd
        mv (i,j) = map (\(di,dj)->(i+di,j+dj)) [(-1,0),(1,0),(0,-1),(0,1)]
