fun main(args: Array<String>) {
    val (h, w) = readIntegerList()
    var costMatrix = Array(10) { readLongList().toTypedArray() }
    warshallFloyd(costMatrix)
    val aMtrix = Array(h) { readIntegerList() }
    var ans = 0L
    for (row in aMtrix) {
        for (i in row) {
            if (i != -1) {
                ans += costMatrix[i][1]
            }
        }
    }
    println(ans)
}

/** O((costMatrix.size)^3) costMatrix is changed */
fun warshallFloyd(costMatrix: Array<Array<Long>>): Array<Array<Long>> {
    val indices = costMatrix.indices
    for (k in indices) {
        for (i in indices) {
            for (j in indices) {
                if (costMatrix[i][k] != Long.MAX_VALUE && costMatrix[k][j] != Long.MAX_VALUE) {
                    costMatrix[i][j] = Math.min(costMatrix[i][j], costMatrix[i][k] + costMatrix[k][j])
                }
            }
        }
    }
    return costMatrix
}


fun readInteger() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readStringList() = readLine()!!.split(" ")
fun readIntegerList() = readStringList().map(String::toInt)
fun readLongList() = readStringList().map(String::toLong)
