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

int pre[2 * MaxN + 5], last[MaxN + 5], other[2 * MaxN + 5], cost[2 * MaxN + 5];
int dis[MaxN + 5], q[MaxN * 10 + 5], a[205][205];
int all, h, w, n;
bool vis[MaxN + 5];

void init(){
	all = -1; memset(last, -1, sizeof(last));
	for(int i = 1; i <= n; i++) dis[i] = 1 << 30;
}

void build(int u, int v, int c){
	pre[++all] = last[u];
	last[u] = all;
	other[all] = v;
	cost[all] = c;
}

void spfa(int p){
	dis[p] = 0;
	int head = 1, tail = 0;
	q[++tail] = p; vis[p] = true;
	while(head <= tail){
		int u = q[head], ed = last[u], dr;
		while(ed != -1){
			dr = other[ed];
			if(dis[u] + cost[ed] < dis[dr]){
				dis[dr] = dis[u] + cost[ed];
				if(!vis[dr]){
					q[++tail] = dr;
					vis[dr] = true;
				}
			}
			ed = pre[ed];
		}
		vis[u] = false;
		head++;
	}
}

int main(){
	int s = 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]);
			build(i, j, a[i][j]);
		}
	}
	for(int i = 1; i <= h * w; i++) {
		init();
		cin >> n;
		if(n > 1) {
			spfa(n);
			s += dis[1];
		}
	}
	cout << s << endl;
	return 0;
}