import Data.Array.IO
import Data.Array.Unboxed
import Control.Monad

type Graph = UArray (Int, Int) Int
type Memo = IOUArray (Int, Int, Int) Int

shortest_path :: Memo -> Graph -> Int -> Int -> Int -> IO Int
shortest_path m g i j 0 = return $ g ! (i, j)
shortest_path m g i j k = do
    v <- readArray m (i, j, k)
    if v /= -1 then return v
    else do
        d  <- shortest_path m g i j (k - 1)
        ik <- shortest_path m g i k (k - 1)
        kj <- shortest_path m g k j (k - 1)
        let ans = min d (ik + kj)
        writeArray m (i, j, k) ans
        return ans

read_int :: String -> Int
read_int = read

tokenize :: String -> [Int]
tokenize = map read_int . words

main = do
    getLine
    c <- forM [0..9] $ \i -> getLine
    let g_aslist = concat $ map tokenize c
    let g = listArray ((0, 0), (9, 9)) g_aslist :: Graph
    memo <- newArray ((0,0,0), (9,9,9)) (-1) :: IO(Memo)
    wall <- filter (\x -> x >= 0) . map read_int . concat . map words . lines <$> getContents
    dists <- mapM (\x -> shortest_path memo g x 1 9) $ wall
    print . sum $ dists

