fun main(args : Array<String>) {
    val (h, w) = readLine()!!.split(" ").map { it.toInt() }
    val cc = Array(10) { readLine()!!.split(" ").map { it.toInt() } }
    val aa = Array(h) { readLine()!!.split(" ").map { it.toInt() } }

    fun findMin(from: Int, min: Int, acc: Int): Int {
        var ret = acc + cc[from][1]

        cc[from].forEachIndexed { idx, i ->
            if (idx == 1) return@forEachIndexed
            if (from == idx) return@forEachIndexed
            if (acc + i >= min) return@forEachIndexed

            ret = Math.min(ret, findMin(idx, min, acc+i))
        }

        return ret
    }

    val minMagics = IntArray(10) {0}

    cc.forEachIndexed { hIdx, cList ->
        if (hIdx == 1) return@forEachIndexed
        minMagics[hIdx] = findMin(hIdx, cc[hIdx][1], 0)
    }

    var magicSum = 0

    aa.forEach { aList ->
        aList.forEach { a ->
            if (a > -1) {
                magicSum += minMagics[a]
            }
        }
    }

    println(magicSum)
}