// D - Wall
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
#define rp(i,s,e) for(int i=(s);i<(e);++i)

void warshall_floyd(vvi &C){
	rp(relay,0,C.size()) rp(from,0,C.size()) rp(to,0,C.size())
		C[from][to] = min(C[from][to], C[from][relay] + C[relay][to]);
}

int main(){
	int H,W; cin>>H>>W;
	vvi C(10, vi(10)), A(H, vi(W));
	rp(i,0,10) rp(j,0,10) cin>>C[i][j];
	warshall_floyd(C);
	int ans = 0;
	rp(h, 0, H) rp(w, 0, W){
		int a; cin>>a;
		if(a >= 0) ans += C[a][1];
	}
	cout<< ans <<endl;
}