#include<bits/stdc++.h>
#define panjang pair<long long, long long>
using namespace std;
long long n, m, a, ans, cost[10], Distance[10][10], banyak = 9 * (1e18 + 7);
int main()
{
    cin>> n >> m;
    for(int i = 0; i <= 9; i++)
        for(int j = 0; j <= 9; j++) cin>> Distance[i][j];

    vector<int> v;

    for(int s = 0; s <= 9; s++)
    {
        for(int i = 1; i <= 9; i++) cost[i] = banyak;

        cost[s] = 0;

        priority_queue< panjang, vector< panjang >, greater< panjang > > Pq;
        Pq.push(make_pair(0, s));
        while(!Pq.empty())
        {
            long long jarak = Pq.top().first;
            long long ind = Pq.top().second;
            Pq.pop();

            if(jarak > cost[ind]) continue;

            for(int i = 1; i <= 9; i++)
            {
                long long wrap = jarak + Distance[ind][i];
                if(wrap < cost[i])
                {
                    cost[i] = wrap;
                    Pq.push(make_pair(wrap, i));
                }
            }
        }
        v.push_back(cost[1]);
    }
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            cin>> a;
            if(a != -1) ans+= v[a];
        }
    cout<< ans << endl;
}
