import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List
import Data.Array
import Data.Array.ST
import Control.Monad.ST

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

main = do
    [h,w] <- getIntList
    cs <- concat <$> replicateM 10 getIntList
    as <- replicateM h getIntList
    let ds :: [((Int,Int),Int)]
        ds = zip [(i,j)| i <- [0..9], j <- [0..9]] cs
    let wf :: Array (Int,Int) Int
        wf = runSTArray $ do
               arr <- newArray ((0,0),(9,9)) 1000
               sequence_ [writeArray arr ij d | (ij,d) <- ds ]
               forM_ [0..9] $ \k -> do
                   forM_ [0..9] $ \i -> do
                       forM_ [0..9] $ \j -> do
                           dij <- readArray arr (i,j)
                           dik <- readArray arr (i,k)
                           dkj <- readArray arr (k,j)
                           writeArray arr (i,j) $ min dij (dik + dkj)
               return arr
    print . sum $ map (sum . (map (\i -> if i == (-1) then 0 else wf ! (i,1)))) as