/*
* Note: This template uses some c++11 functions , so you have to compile it with c++11 flag.
*       Example:-   $ g++ -std=c++11 c++Template.cpp
*
* Author : Akshay Pratap Singh
* Handle: code_crack_01
*
*/
 
/********   All Required Header Files ********/
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define SCD(t) scanf("%d",&t)
#define SCLD(t) scanf("%ld",&t)
#define SCLLD(t) scanf("%lld",&t)
#define SCC(t) scanf("%c",&t)
#define SCS(t) scanf("%s",t)
#define SCF(t) scanf("%f",&t)
#define SCLF(t) scanf("%lf",&t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for(int i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for(int i=j ; i>=k ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
typedef long long int ll;
typedef unsigned long long int  ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<ll> vll;
typedef vector<vi> vvi;

const int MAX = 10;
const int INFTY = (1<<20);
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int n;
vector<pii> adj[MAX];
int color[MAX];
int d[MAX];

void dijkstra(){

    priority_queue<pii> PQ;
    REP(i, n){
        d[i] = INFTY;
        color[i] = WHITE;
    }

    d[1] = 0;
    PQ.push(make_pair(0,1));
    color[1] = GRAY;

    while( !PQ.empty() ){
        pii f = PQ.top(); PQ.pop();
        int u = f.second;

        color[u] = BLACK;

        if( d[u] < f.first * (-1) ) continue;

        REP(j, adj[u].size()){
            int v = adj[u][j].first;
            if ( color[v] == BLACK )  continue;
            if ( d[v] > d[u] + adj[u][j].second ) {
                d[v] = d[u] + adj[u][j].second;
                PQ.push(make_pair(d[v]*(-1), v));
                color[v] = GRAY;
            }
        }
    }

    // REP(i, n){
    //     cout << i << " " << ( d[i] == INFTY ? -1 : d[i] ) << endl;
    // }

}

int main(){

    int h, w; cin >> h >> w;
    int c;
    n = 10;
    REP(i, 10){
        REP(j, 10){
            cin >> c;
            if( c < 0 ) c = INFTY;
            adj[j].push_back(make_pair(i, c));
        }
    }

    dijkstra();

    ll ans = 0;
    REP(i, h){
        REP(j, w){
            cin >> c;
            if( c>=0 ) ans += d[c];
        }
    }

    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256