import kotlin.math.min

fun main(args: Array<String>){

    var (H, W) = readLine()!!.split(" ").map{it.toInt()}
    var c = Array(10){IntArray(10)}
    var A = Array(H){IntArray(W)}
    var countNum= IntArray(10){0}
    var ans = 0L
    for(i in c.indices){
        c[i] = readLine()!!.split(" ").map{it.toInt()}.toIntArray()
    }
    for(i in A.indices){
        A[i] = readLine()!!.split(" ").map{it.toInt()}.toIntArray()
    }
    var minToOne = IntArray(10){1001}
    minToOne[1] = 0

    var minRoot = 1
    var checked = Array(10){false}
    checked[1] = true
    var allchecked = false
    while(!allchecked) {
        var min = 1001
        var minIndex = -1
        for (i in 0 until 10) {
            if (!checked[i]) {
                minToOne[i] = Math.min(c[i][minRoot] + minToOne[minRoot], minToOne[i])
                if (min > minToOne[i]) {
                    min = minToOne[i]
                    minIndex = i
                }
            }
        }
        if(minIndex == -1) break

        checked[minIndex] = true
        minRoot = minIndex
        allchecked = true
        for(i in checked.indices){
            if(!checked[i]){
                allchecked = false
            }
        }
    }

    for (i in 0 until H) {
            for (j in 0 until W) {
                if (A[i][j] != -1) {
                    countNum[A[i][j]]++
                }
            }
        }

    for(i in countNum.indices){
        ans += countNum[i] * minToOne[i]
    }

    println(ans)
}