#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <iostream>

using namespace std;
const int MaxN = 2e4;

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

void build(int u, int v, int c){
	pre[++all] = last[u];
	last[u] = all;
	other[all] = v;
	cost[all] = c;
}
 
void SPFA(int s) {
	for(int i = 1; i <= h * w; i++) dis[i] = 1 << 30;
	dis[s] = 0;
	int head = 1, tail = 0;
	seq[++tail] = s, vis[s] = true;
	while(head <= tail){
		int now = seq[head], ed = last[now], dr;
		while(ed != -1){
			dr = other[ed];
			if(dis[now] + cost[ed] < dis[dr] ){
				dis[dr] = dis[now] + cost[ed];
				if(vis[dr] == 0)
					seq[++tail] = dr, vis[dr] = true;
			}
			ed = pre[ed];
		}
		vis[now] = false;
		head++;
	}
}
int main()
{
	int s = 0, n;
	all = -1;
	memset(last, -1, sizeof(last));
	cin >> h >> w;
	for(int i = 0; i <= 9; i++) {
		for(int j = 0; j <= 9; j++) {
			cin >> a[i][j];
			build(i, j, a[i][j]);
		}
	}
	for(int i = 1; i <= h * w; i++) {
		cin >> n;
		if(n > 1) {
			SPFA(n);
			s += dis[1];
		}
		cout << s << endl;
	}
	cout << s << endl;
	return 0;
}