#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define ll long long
#define ull unsigned long long
#define ld long double
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define rbf(a, x) for (auto& a : x)
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define drep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define pll pair<long long,long long>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vvc vector<vector<char>>
#define vc vector<char>
#define vvb vector<vector<bool>>
#define vb vector<bool>
#define Maxe(x) *max_element(all(x))
#define Mine(x) *min_element(all(x))
#define Size(x) ((ll)(x).size())
#define umap unordered_map
const long long INF = 1LL << 60;
const long double pi = 3.1415926535897932;
#define Fi first
#define Se second
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

    template<typename Num>
        constexpr Num mypow(Num a, long long b) {
            if(b==0)return 1;
            if (a==0)return 0;
            Num x = 1;
              while (b > 0) {
                if(b & 1)x*=a;
                a*=a;
                b >>= 1;
              }
              return x;
        }

    template<class T> inline bool chmin(T& a, T b) {
        if (a > b) {
            a = b;
            return true;
        }
        return false;
    }
    template<class T> inline bool chmax(T& a, T b) {
        if (a < b) {
            a = b;
            return true;
        }
        return false;
    }

//lis,modpow,uf,modncr,soinsubunkai,yakusurekkyo
//mint,dijkstra,gyakugen

    //graph g(n);で作成
    //g.add_edge(a,b,cost);
    //g.dijkstra(p);g.d[i]でpとiとの距離
    struct edge{
        ll to,cost;
    };
    
    struct graph{
        ll V;
        vector<vector<edge>> G;
        vl d;
        graph(ll n){
            init(n);
        }
        void init(ll n){
            V=n;
            G.resize(V);
            d.resize(V);
            rep(i,V){
                d[i]=INF;
            }
        }
        void add_edge(ll s,ll t,ll cost){
            edge e;
            e.to=t;
            e.cost=cost;
            G[s].pb(e);
        }
        void dijkstra(ll s){
            rep(i,V){
                d[i]=INF;
            }
            d[s]=0;
            priority_queue<pll,vector<pll>,greater<pll>> que;
            que.push(pll(0,s));
            while(!que.empty()){
                pll p=que.top();
                que.pop();
                ll v=p.second;
                if(d[v]<p.first)continue;
                for(auto e:G[v]){
                    if(d[e.to]>d[v]+e.cost){
                        d[e.to]=d[v]+e.cost;
                        que.push(pll(d[e.to],e.to));
                    }
                }
            }
        }
    };

    signed main(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        ll h,w;
        cin>>h>>w;
        graph G(10);
        rep(i,10){
            rep(j,10){
                ll c;
                cin>>c;
                G.add_edge(i,j,c);
            }
        }
        umap<ll,ll> m;
        rep(i,h*w){
            ll a;
            cin>>a;
            m[a]++;
        }
        ll res=0;
        rep(i,10){
            G.dijkstra(i);
            res+=G.d[1]*m[i];
        }
        cout<<res<<"\n";
    }

Measured memory_bytes: 3668