import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val H, W = in.next().toInt
  val c = Array.ofDim[Int](10, 10)
  val A = Array.ofDim[Int](H+1, W+1)

  for(y <- 0 to 9)
    for(x <- 0 to 9)
      c(y)(x) = in.next().toInt
  for(y <- 1 to H; x <- 1 to W)
    A(y)(x) = in.next().toInt


  //
  // d[a][b] は辺 e = (a, b) のコスト(存在しない場合はINF, ただし d[i][i] = 0 とする)
  val d = Array.ofDim[Long](10, 10)

  // 初期化
  val MAX = Long.MaxValue
  for(i <- 0 to 9; j <- 0 to 9){
    if(i == j)
      d(i)(j) = 0
    else
      d(i)(j) = MAX
  }

  // ここで d の入力行う
  for(y <- 0 to 9)
    for(x <- 0 to 9)
      d(y)(x) = c(y)(x)


  // warshall_floyd
  for(k <- 0 to 9; i <- 0 to 9; j <- 0 to 9){
    d(i)(j) = Math.min(d(i)(j), d(i)(k) + d(k)(j))
  }



  val memo = new Array[Int](10)
  for(y <- 1 to H; x <- 1 to W)
    if(A(y)(x) != -1)
      memo(A(y)(x)) += 1


  var ans = 0L
  for(i <- 0 to 9){
    if(i != 1)
      ans += d(i)(1) * memo(i)
  }

  println(ans)
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
