import Control.Monad
import Control.Applicative
import Data.Functor
import Data.List
import Data.List.Split
import Data.Char
import Data.Ord
import Data.Array.IO
import Data.IORef

readInt = read :: String -> Int
getInt = readLn :: IO Int
getInts = map readInt . words <$> getLine
sjoin :: (Show a) => [a] -> String
sjoin = unwords . map show
cond :: a -> a -> Bool -> a
cond t f c = if c then t else f
-- end of templete

wf arr range = forM_ range $ \k -> forM_ range $ \i -> forM_ range $ \j -> update <$> readArray arr (i,j) <*> readArray arr (i,k) <*> readArray arr (k,j) >>= writeArray arr (i,j)
  where
    update a b1 b2 = min a $ b1 + b2

cost arr (-1) = return 0
cost arr n = readArray arr (n,1)

main = do
    [h, w] <- getInts
    arr <- concat <$> replicateM 10 getInts >>= newListArray ((0,0),(9,9)) :: IO (IOUArray (Int,Int) Int)
    wf arr [0..9]
    concat <$> replicateM h getInts >>= sequence . map (cost arr) >>= print . sum

