#include<bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
#define int long long

int H, W;
int c[10][10];
int ans = 0;

void warshall_floyd() {
  for (int k = 0; k < 10; k++) {
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
      }
    }
  }
}

signed main(){
  cin >> H >> W;
  rep(i,10)rep(j,10)cin >> c[i][j];
  warshall_floyd();
  rep(i,H)rep(j,W){int a;cin >> a;if(a==-1)continue;ans+=c[a][1];}
  cout << ans;
}
