fun main(arg: Array<String>) {
    val (H, W) = listOfInt()
    val C = Array(10) { listOfInt().toIntArray() }
    val A = Array(H) { listOfInt().toIntArray() }
    for (k in 0 .. 9) for (h in 0 .. 9) for (w in 0 .. 9)
        if (C[h][w] > C[h][k] + C[k][w]) C[h][w] = C[h][k] + C[k][w]
    val ans = A.sumBy { it.sumBy { if (it != -1) C[it][1] else 0 } }
    println(ans)
}

fun next() = readLine()!!
fun nextInt(delta: Int = 0) = Integer.parseInt(next()) + delta
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { Integer.parseInt(it) }
fun listOfLong() = listOfString().map(String::toLong)
