#include <bits/stdc++.h>
using namespace std;
int h,k,mp[10][10];
int main(){
	scanf("%d%d",&h,&k);
	for (int i=0;i<10;++i){
		for (int j=0;j<10;++j)
		scanf("%d",&mp[i][j]);
	}
	for (int k1=0;k1<10;++k1){
		for (int i=0;i<10;++i){
			for (int j=0;j<10;++j){
				if (mp[i][k1]+mp[k1][j]<mp[i][j])mp[i][j]=mp[i][k1]+mp[k1][j];
			}
		}
	}
	int cnt=0;
	for (int i=0;i<h;++i){
		for (int j=0;j<k;++j){
			int s;
			scanf("%d",&s);
			if (s == (-1))continue;
			cnt+=mp[s][1];
		}
	}
	printf("%d\n",cnt);
	return 0;
}