#define rep(i, n) for(ll i = 0; i < n; i++)
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
	int h,w;
	cin >> h >> w;
	int c[10][10];
	rep(i,10) rep(j,10) {
		cin >> c[i][j];
	}
	rep(k,10) rep(i,10) rep(j,10) {
		if (c[i][j]>c[i][k]+c[k][j]) c[i][j] = c[i][k] + c[k][j];
	}

	int ans = 0;
	rep(i,h) rep(j,w) {
		int a;
		cin >> a;
		if (a>=0) ans += c[a][1];
	}
	cout << ans << endl;
	return 0;
}
