import qualified Data.Map as Map

main = do
    [h,w] <- map read.words <$> getLine
    contents <- lines <$> getContents

    let tizu = Map.fromList (zip [(i,j)| i <- [1..h], j <- [1..w]] (concat contents))
    let num_white = sum [1 | x <- (concat contents), x == '.']

    print $ points (h,w) tizu num_white [((1,1),1)] []


points (h,w) tizu num_white [] visited = -1
points (h,w) tizu num_white (((i,j),k):check) visited
    | or [i<1,i>h,j<1,j>w] = points (h,w) tizu num_white check visited
    | (i,j) == (h,w) = num_white - k
    | (i,j) `elem` visited = points (h,w) tizu num_white check visited
    | tizu Map.! (i,j) == '#' = points (h,w) tizu num_white check visited
    | otherwise = points (h,w) tizu num_white (check ++ [((i+1,j),k+1),((i-1,j),k+1),((i,j+1),k+1),((i,j-1),k+1)]) ((i,j):visited)