'use struct'

const input = require('fs').readFileSync('/dev/stdin', 'utf8')
const lines = input.split('\n')
const hw = lines.shift().split(' ')
const h = parseInt(hw[0], 10)
// const w = parseInt(hw[1], 10)
const dist = []
for (var i = 0; i < 10; i++) {
	dist.push(
		lines
			.shift()
			.split(' ')
			.map(v => parseInt(v, 10))
	)
}
// console.log(dist)
const counts = new Array(10).fill(0)
for (var i = 0; i < h; i++) {
	lines
		.shift()
		.split(' ')
		.map(v => parseInt(v, 10))
		.forEach(v => counts[v]++)
}

for (var k = 0; k < 10; ++k) {
	for (var i = 0; i < 10; ++i) {
		for (var j = 0; j < 10; ++j) {
			dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j])
		}
	}
}
// console.log(dist)

var sum = 0
for (var i = 0; i < 10; i++) {
	sum += counts[i] * dist[i][1]
}
console.log(sum)
