import           Control.Monad
import qualified Data.Array.IO         as IO
import qualified Data.ByteString.Char8 as BS
import           Data.Maybe
import qualified Data.Sequence         as Seq

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
  [h, w] <- getIntList
  s <- replicateM h getLine

  let inf = 10^9 :: Int

  -- (distance >= 0 , direction = 0, 1, 2, 3)
  -- direction : from N, S, W, E
  g <- IO.newArray ((0,0),(h+1,w+1)) (0,0) :: IO (IO.IOArray (Int, Int) (Int,Int))
  forM_ [1..h] $ \i -> do
    let s_i = s !! (i-1)
    forM_ [1..w] $ \j -> do
      let s_ij = s_i !! (j-1)
          d = if s_ij == '.' then inf else 0
      IO.writeArray g (i,j) (d, 0)

  let bsf :: Seq.Seq (Int,Int) -> Int -> IO ()
      bsf q dist | Seq.length q == 0 = return ()
                 | otherwise = do
                     let (y, x) Seq.:< q' = Seq.viewl q
                     ps <- forM [0..3] $ \i -> do
                       let (dy, dx) = [(1,0),(-1,0),(0,1),(0,-1)] !! i
                           (y', x') = (y+dy, x+dx)
                           dist' = dist + 1
                       (t, u) <- IO.readArray g (y', x')
                       if (dist' < t)
                         then do IO.writeArray g (y', x') (dist', i)
                                 return [(y', x')]
                         else return []
                     let q'' = q' Seq.>< (Seq.fromList $ concat ps)
                     bsf q'' (dist+1)

      q = Seq.fromList [(1,1)]

  IO.writeArray g (1,1) (0,0)
  bsf q 0

  let findPath :: Int -> Int -> IO ()
      findPath y x | (y,x) == (1,1) = return ()
                   | otherwise = do
                       (t, u) <- IO.readArray g (y, x)
                       IO.writeArray g (y, x) (0, u)
                       let (dy, dx) = [(-1,0),(1,0),(0,-1),(0,1)] !! u
                       findPath (y+dy) (x+dx)

      countPt :: Int -> Int -> Int -> IO Int
      countPt y x res | y > h = return res
                      | otherwise = do
                          (t, _) <- IO.readArray g (y, x)
                          let x' = if x <= w then x + 1 else 1
                              y' = if x <= w then y else y + 1
                              res' = res + (if t > 0 then 1 else 0)
                          countPt y' x' res'
  findPath h w
  ans <- countPt 1 1 0

  print ans