#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
int main() {
    int h, w; cin >> h >> w;
    vector<vector<int>> d(10, vector<int>(10));
    rep(i, 10) rep(j, 10) cin >> d[i][j];
    rep(k, 10) rep(i, 10) rep(j, 10)
    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    int ans = 0;
    rep(i, h) rep(j, w) {
        int x; cin >> x;
        if(x!=-1) ans += d[x][1];
    }
    cout << ans;
}
