import java.util.concurrent.LinkedBlockingDeque

fun intList() = readLine()?.split(" ")?.map(String::toInt) ?: TODO()
fun main() {
    data class Coordinate(
        val i: Int,
        val costs: Int
    )
    val (h, w) = intList()
    val matrix = Array(10) { Array(10) { 0 } }
    repeat(10) { i ->
        intList().forEachIndexed { j, v ->
            matrix[i][j] = v
        }
    }
    val numbers = hashMapOf<Int, Int>()
    repeat(h) {
        intList().forEach {
            numbers[it] = numbers.getOrDefault(it, 0) + 1
        }
    }
    var magic = 0
    numbers.keys.forEach {
        if (it != 1 && it != -1) {
            val queue = LinkedBlockingDeque<Coordinate>()
            var minCost = matrix[it][1]
            queue.offer(
                Coordinate(
                    it, 0
                )
            )
            while (!queue.isEmpty()) {
                val c = queue.poll()
                matrix[c.i].forEachIndexed { index, v ->
                    if (v != 0) {
                        val cost = c.costs + v
                        if (minCost > cost) {
                            if (index == 1) {
                                minCost = cost
                            }
                            queue.offer(
                                Coordinate(index, cost)
                            )
                        }
                    }
                }
            }
            magic += minCost * numbers.getOrDefault(it, 0)
        }
    }
    println(magic.toString())
}