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 chk = Set.singleton (1,1)
    let q0 = Sq.fromList [(1,1)]
    let q =  q0 <| Sq.empty
    let white = BC.length $ BC.filter(=='.') s
   
    if (Sq.length (move q h w grid chk)) == 0 then print (-1)
                                          else print $ white -  Sq.length (move q h w grid chk) 
    
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

--movestep :: [(Int,Int)] -> Int -> Int -> (array (Int,Int)) -> [[(Int,Int)]]
movestep qa h w grid chk = [(i,j) | (i,j) <- (adjblock (sqright qa) h w ) , (grid ! (i,j)) == '.' , Set.member (i,j) chk == False]

--move :: (Seq [[(Int,Int)]]) -> Int -> Int -> (Array (Int,Int)) -> [(Int,Int)]
move q h w grid chk = if Sq.length q == 0
                      then Sq.empty
                      else if sqright (sqleft q) == (h,w)
                           then sqleft q
                           else move q2 h w grid chk2
                                where qa = sqleft q
                                      q2 = (Sq.drop 1 q) >< (Sq.fromList add)
                                      add = [qa |> x |x <- (movestep qa h w grid chk)]
                                      chk2 = Set.union chk (Set.fromList (movestep qa h w grid chk))