import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Map as Map
import Data.Array
import qualified Data.ByteString.Char8 as BC
import Data.Maybe
import qualified Data.Set as Set
import qualified Data.Sequence as Sq
import Data.Sequence (ViewL(..),ViewR(..),(><),(<|),(|>),Seq)

bsreadInt :: BC.ByteString -> Int
bsreadInt = fst . fromJust . BC.readInt
strToInt s = (read :: String -> Int) s

main = do

    [h,w]<- map bsreadInt . BC.words <$> BC.getLine
    ss <- replicateM h BC.getLine
    let s = BC.filter(/='\r') $ BC.concat ss
    let grid = array ((1,1),(h,w)) [((i,j), BC.index s ((i-1) * w + (j-1))) | i<-[1..h],j<-[1..w]]
    
    let q =  Sq.fromList [[(1,1)]]
    let white = BC.length $ BC.filter(=='.') s
    if length (move q h w grid) == 0 then print (-1)
                                     else print $ white -  length (move q h w grid) 
    
adjblock :: (Int,Int) -> Int -> Int -> [(Int,Int)]
adjblock (m,n) h w
   | m==1 && n==1 = [(2,1),(1,2)]
   | m==h && n==w = []
   | m==h && n==1 = [(m-1,n),(m,n+1)]
   | m==1 && n==w = [(m+1,n),(m,n-1)]
   | m==1 = [(m+1,n),(m,n-1),(m,n+1)]
   | n==1 = [(m-1,n),(m+1,n),(m,n+1)]
   | m==h = [(m-1,n),(m,n-1),(m,n+1)]
   | n==w = [(m-1,n),(m+1,n),(m,n-1)]
   | otherwise = [(m-1,n),(m+1,n),(m,n-1),(m,n+1)]

sqleft :: Seq a -> a
sqleft q = sqleft_(Sq.viewl q)
sqleft_ :: ViewL a -> a
sqleft_ (x :< xs) = x
sqright :: Seq a -> a
sqright q = sqright_(Sq.viewr q)
sqright_ :: ViewR a -> a
sqright_ (xs :> x) = x

sqlistaddr :: Seq a -> [a] -> Seq a
sqlistaddr q [] = q
sqlistaddr q (a:an) = sqlistaddr (q |> a) an


--movestep :: [(Int,Int)] -> Int -> Int -> (array (Int,Int)) -> [[(Int,Int)]]
movestep (a:an) h w grid = [(i,j) | (i,j) <- (adjblock a h w ) , (grid ! (i,j)) == '.' , elem (i,j) (a:an) == False]

--move :: (Seq [[(Int,Int)]]) -> Int -> Int -> (Array (Int,Int)) -> [(Int,Int)]
move q h w grid = if Sq.length q == 0
                  then []
                  else if head (sqleft q) == (h,w)
                       then sqleft q
                       else move q2 h w grid
                            where qa = sqleft q
                                  q2 = (Sq.drop 1 q) >< addque
                                  addque = Sq.fromList [i : qa | i <- (movestep qa h w grid)]