object Main extends App {
	import scala.math._

	val NH = scala.io.StdIn.readLine.split(" ").map(_.toInt)
	val c = Array.ofDim[Int](10,10)

	for(i<-0 to 9){
		c(i) = scala.io.StdIn.readLine.split(" ").map(_.toInt)
	}

	for(i<-0 to 9){
		for(j<-0 to 9){
			for(k<-0 to 9){
				c(i)(j) = min(c(i)(j), c(i)(k)+c(k)(j))
			}
		}
	}

	val a = new Array[Int](NH(0))

	for(i<-0 until NH(0)){
		a(i) = scala.io.StdIn.readLine.split(" ").map(_.toInt).map(z => if(z == -1) 0 else c(z)(1)).sum
	}

	println(a.sum)


}