import java.util.Scanner

class Graph(n: Int, reversible: Boolean) {
  private val cost = Array.ofDim[Int](n, n)
  private val connected = Array.fill[Boolean](n, n)(false)
  private val N = n
  private val Reversible = reversible

  def connect(from: Int, to: Int, cost: Int): Unit = {
    connected(from)(to) = true
    this.cost(from)(to) = cost

    if (Reversible) {
      connected(to)(from) = true
      this.cost(to)(from) = cost
    }
  }

  def warshall_floyd(): Unit = {
    (0 until N).foreach(i => {
      (0 until N).foreach(j => {
        (0 until N).foreach(k => {
          if (connected(i)(j))
            cost(i)(j) = math.min(cost(i)(j), cost(i)(k) + cost(k)(j))
        })
      })
    })
  }

  def getCost(from: Int)(to: Int): Int = {
    cost(from)(to)
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)

    val H, W = sc.nextInt
    val g = new Graph(10, false)
    (0 to 9).foreach(i => {
      (0 to 9).foreach(j => {
        g.connect(i, j, sc.nextInt)
      })
    })

    g.warshall_floyd()

    var ans = 0
    (0 until H).foreach(_ => {
      (0 until W).foreach(_ => {
        val x = sc.nextInt
        if (x != -1) {
          ans += g.getCost(x)(1)
        }
      })
    })

    println(ans)
  }
}