import java.io.{BufferedReader, IOException, InputStream, InputStreamReader}
import java.util.StringTokenizer

import scala.collection.mutable
import scala.util.Sorting

object Main {
  def solve(): Unit = {
    val sc = new InputReader(System.in)
    val H = sc.nextInt()
    val W = sc.nextInt()

    val INF = 10010
    val c = Array.ofDim[Int](10, 10)
    for {
      i <- 0 to 9
      j <- 0 to 9
    } c(i)(j) = sc.nextInt()

    val dp: Array[Int] = Array.fill(10)(INF)
    dp(1) = 0
    for {
      _ <- 0 until 10
      i <- 0 to 9
      j <- 0 to 9
    } {
      dp(i) = math.min(dp(i), dp(j) + c(i)(j))
    }

    val ans = (for {
      _ <- 0 until H
      _ <- 0 until W
    } yield {
      sc.nextInt() match {
        case -1 | 1 => 0
        case a => dp(a)
      }
    }).sum

    println(ans)
  }

  def main(args: Array[String]): Unit = {
    solve()
  }

  class InputReader(val stream: InputStream) {
    val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    var tokenizer: StringTokenizer = null

    def next: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next.toInt
    def nextChar(): Char = next.charAt(0)
  }

}
