{-# LANGUAGE FlexibleContexts, RankNTypes #-}
import Control.Arrow
import Control.Monad
import Control.Monad.ST
import Data.List
import qualified Data.ByteString.Char8 as B
import qualified Data.Vector as V 
import qualified Data.Vector.Mutable as MV

main :: IO ()
main = readLine >>= main'
    where
        main' xs = inputs xs >>= print . uncurry solve 
        inputs [h, w] = (,) <$> readContents 10 <*> readContents h

solve :: [[Int]] -> [[Int]] -> Int
solve cs = sum . map (sum . map count) 
    where
        count a 
            | a > -1 = value a
            | otherwise = 0 

        value a = vs V.! (10 * a + 1)

        vs = V.create (initialize >>= calc)
            where
                initialize = (MV.new 100 :: ST s (MV.STVector s Int)) >>= (\xs -> xs <$ mapM_ (uncurry (MV.unsafeWrite xs)) (zip [0..] $ concat cs)) 
                calc xs = xs <$ mapM_ (check xs) [(i, j, k) | i <- [0..9], j <- [0..9], k <- [0..9]]
                check xs (i, j, k) = inputs >>= br
                    where
                        inputs = (,,) <$> MV.unsafeRead xs (10 * i + j) <*> MV.unsafeRead xs (10 * i + k) <*> MV.unsafeRead xs (10 * k + j)
                        br (a, b, c) 
                            | b + c < a = MV.unsafeWrite xs (10 * i + j) $ b + c 
                            | otherwise = return ()

readLine :: IO [Int]
readLine = unfoldr (fmap (second (maybe B.empty snd . B.uncons)) . B.readInt) <$> B.getLine

readContents :: Int -> IO [[Int]]
readContents = flip replicateM readLine