#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
	ll h, w;
	cin >> h >> w;
	ll ans = 0;
	vector<vector<ll>> c(10, vector<ll>(10));
	for (ll i = 0; i < 10; ++i) {
		for (ll j = 0; j < 10; ++j) {
			cin >> c[i][j];
		}
	}
	for (ll k = 0; k < 10; ++k) {
		for (ll i = 0; i < 10; ++i) {
			for (ll j = 0; j < 10; ++j) {
				c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
			}
		}
	}
	for (ll i = 0; i < h * w; ++i) {
		int a;
		cin >> a;
		if (a != -1)ans += c[a][1];
	}
	cout << ans << endl;
}