object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val h, w = sc.nextInt()
  val n = 10
  val c = Array.fill(n, n)(sc.nextInt())
  val a = List.fill(h * w)(sc.nextInt())

  for {
    k <- 0 until n
    i <- 0 until n
    j <- 0 until n
    if c(i)(k) + c(k)(j) < c(i)(j)
  } {
    c(i)(j) = c(i)(k) + c(k)(j)
  }

  val count = a.groupBy(identity).view.mapValues(_.length).toMap.withDefaultValue(0)
  val ans = (0 until n).map(i => c(i)(1) * count(i)).sum
  println(ans)
}
