#include <cstdio>
#include <algorithm>

int f[210][210];

int main()
{
	int h,w;
	scanf("%d%d",&h,&w);
	for (int i = 0; i<=9; i++)
		for (int j = 0; j<=9; j++)
			scanf("%d",&f[i][j]);


	for (int k = 0; k<=9; k++)
		for (int i = 0; i<=9; i++)
			for (int j = 0; j<=9; j++)
				f[i][j] = std::min(f[i][j],f[i][k] + f[k][j]);

	int ans = 0;
	for (int i = 1; i<=h; i++)
		for (int j = 1; j<=w; j++)
		{
			int x;
			scanf("%d",&x);
			if (x>=0)
				ans += f[x][1];
		}

	printf("%d",ans);
}