-- 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 :: Graph -> Int -> Int -> Int -> Int
shortest_path g i j 0 = g ! (i, j)
shortest_path g i j k =
    let d  = shortest_path g i j (k - 1)
        ik = shortest_path g i k (k - 1)
        kj = shortest_path g k j (k - 1)
    in min d (ik + kj)


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
    wall <- map (abs . read_int) . concat . map words . lines <$> getContents
    print $ sum . map (\x -> shortest_path g x 1 9) $ wall

