#include <bits/stdc++.h>
 
using namespace std;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
 
typedef long long ll;
 
const ll mod = 1e9+7;
const double pi = acos(-1);

int n,t,j,i,k;
int m, c[12][12];
int f[11],bc[11];
int dp[12][12];


int main(){
	memset(f, 0, sizeof f);
	scanf("%d %d", &n, &m);
	for(i=0;i<10;i++){
		for(j=0;j<10;j++){
			scanf("%d", c[i]+j);
			dp[i][j] = c[i][j];
		}
	}
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			scanf("%d", &t);
			if(t>=0)
				f[t]++;
		}
	}
	for(i=0;i<10;i++){
		for(j=0;j<10;j++){
			for(k=0;k<10;k++){
				if(dp[i][j] > dp[i][k]+dp[k][j])
					dp[i][j] = dp[i][k]+dp[k][j];
			}
			cout<<dp[i][j]<<" ";
		}
		cout<<endl;
	}
	int ans =0;
	for(i=0;i<10;i++)
		ans += f[i]*dp[i][1];
	printf("%d\n", ans);
	return 0;
}