fun main(args: Array<String>) {
    val (H, W) = readLine()!!.split(" ").map { it.toInt() }
    val c = mutableListOf<LongArray>()
    (0..9).forEach {
        val ci = readLine()!!.split(" ").map { it.toLong() }.toLongArray()
        c.add(ci)
    }

    val A = mutableListOf<IntArray>()
    (1..H).forEach {
        val a = readLine()!!.split(" ").map { it.toInt() }.toIntArray()
        A.add(a)
    }

    fun worshallFloyd() {
        // TODO CAUTION order i, j, k
        (0..9).forEach { i ->
            (0..9).forEach { j ->
                (0..9).forEach { k ->
                    c[j][k] = Math.min(c[j][k], c[j][i] + c[i][k])
                }
            }
        }
    }

    // O(10^3)
    worshallFloyd()
    var minCost = 0L
    // O(HW)
    A.forEach {
        it.forEach {a ->
            if (a >= 0) {
                minCost += c[a][1]
            }
        }
    }
    println(minCost)
}
