{-# OPTIONS_GHC -O #-}
{-# LANGUAGE ViewPatterns #-}

import Control.Monad
import Control.Monad.ST
import Data.Maybe
import Data.Array.Unboxed
import Data.Array.ST
import qualified Data.Sequence as SQ
import Data.Sequence ((|>), (<|), ViewL(..), ViewR(..))

main = do
    (h:w:_)<-map read.words<$>getLine::IO [Int]
    m <- concat <$> replicateM h getLine :: IO String
    --
    let wn = length $ filter(=='.') m
    let bd = sol' h w $ listArray ((1,1),(h,w)) $ map (\c->if c=='.' then 0 else (-1)) m::UArray (Int,Int) Int
    let g = bd ! (h,w)
    print $ if g == 0 then (-1) else wn - g
--
sol' :: Int -> Int -> UArray (Int,Int) Int -> UArray (Int,Int) Int
sol' h w bd = runSTUArray $ do
    m <- thaw bd :: ST s (STUArray s (Int,Int) Int)
    solve h w m $ SQ.singleton ((1,1),1)
    return m

solve :: Int -> Int -> STUArray s (Int,Int) Int -> SQ.Seq ((Int,Int),Int) -> ST s ()
solve _ _ _ (SQ.viewl -> SQ.EmptyL) = return ()
solve h w m (SQ.viewl -> ((i,j),s) :< sq)
    | i<1 || i>h || j<1 || j>w = solve h w m sq
    | otherwise = do
        x <- readArray m (i,j)
        if x /= 0 then solve h w m sq
        else do
            writeArray m (i,j) s
            solve h w m $ foldl (|>) sq $ zip [(i-1,j),(i+1,j), (i,j-1), (i,j+1)] $ repeat (s+1)
