#include<cstdio> 
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring> 
using namespace std;
const int MaxN = 200;

int a[MaxN + 5][MaxN + 5],b[MaxN + 5][MaxN + 5];

void floyd()
{
	for(int k = 0;k <= 9; k++)
		for(int i = 0;i <= 9; i++)
			for(int j = 0;j <= 9; j++)
				//a[i][j] = a[i][j] || (a[i][k] && a[k][j]);
				a[i][j] = min(a[i][j],a[i][k] + a[k][j]);
}

int main()
{
	int h,w,z;
	int ans = 0;
	scanf("%d %d",&h,&w);
	for(int i = 0;i <= 9;i++)
	{
		for(int j = 0;j <= 9;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	floyd();
	for(int i = 1;i <= h;i++)
	{
		for(int j = 1;j <= w;j++)
		{
			scanf("%d",&b[i][j]);
			z = b[i][j];
			if(z==-1) continue;
			ans += a[z][1];
		}
	}
	printf("%d\n",ans);
	return 0;
}