fun main(args: Array<String>) = yorukatsu11d()

fun yorukatsu11d() {
    val (h, w) = readLine()!!.split(' ').map { it.toInt() }
    val walls = (0..9).map {
        readLine()!!.split(' ').map { it.toInt() }.toIntArray()
    }.toTypedArray()
    val queries = (1..h).map { readLine()!!.split(' ').map { it.toInt() } }
        .flatten().filter { it != 1 && it != -1 }.toIntArray()

    for (k in 0..9)
        for (i in 0..9)
            for (j in 0..9) walls[i][j] = Math.min(walls[i][j], walls[i][k] + walls[k][j])

    val answer = queries.map { walls[it][1] }.sum()

    println(answer)
}
