//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <limits>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define EXISTch(s,c) ((((s).find_first_of(c)) != std::string::npos)? 1 : 0)//cがあれば1 if(1)
#define SORT(c) sort((c).begin(),(c).end())

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = (int)1000000007;
const LL MOD = (LL)1000000007;//10^9+7
const LL INF2 = (LL)100000000000000000;//10^18

//O(V^3) V=300程度か
template< typename T >
using Matrix = vector< vector< T > >;

template< typename T >
void warshall_floyd(Matrix< T > &g, T INF) {
	for (int k = 0; k < g.size(); k++) {
		for (int i = 0; i < g.size(); i++) {
			for (int j = 0; j < g.size(); j++) {
				//負の辺+INFをしてしまうとバグるので
				if (g[i][k] == INF || g[k][j] == INF) continue;
				g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
			}
		}
	}
}

//最短距離がマイナスになっていると負の閉路がある
//for (int i = 0; i < V; i++) {
//	if (mat[i][i] < 0) {
//		puts("NEGATIVE CYCLE");
//		return 0;
//	}
//}

//結果を改行して表示

//for(int i = 0; i < V; i++) {
//	for (int i = 0; i < V; i++) {
//		if (mat[i][j] == INT_MAX) {
//			cout << "INF" << endl;
//		}
//		else {
//			cout << mat[i][j] << endl;
//		}
//	}
//}


//for (int i = 0; i < V; i++) {
//	for (int i = 0; i < V; i++) {
//
//	}
//}

int main() {
	int h, w; cin >> h >> w;


	int V = 10;
	
	Matrix< int > mat(V, vector< int >(V, INT_MAX));
	for (int i = 0; i < V; i++) mat[i][i] = 0;

	for (int i = 0; i < V; i++) {
		for (int j = 0; j < V; j++) {
			cin >> mat[i][j];
		}
	}

	warshall_floyd(mat, INT_MAX);

	LL ans = 0;
	for (int i = 0; i < h; i++) {
		for (int i = 0; i < w; i++) {
			int x; cin >> x;
			if (x == -1)continue;

			ans += mat[x][1];
		}
	}

	cout << ans << endl;

	//各辺についてのなにか
	//到達不可能
	//mat[i][j] == INT_MAX
	//負の閉路条件
	//mat[i][i] < 0

	//mat[x[i]][y[i]] == z[i];
	//mat[y[i]][x[i]] == z[i];

	return 0;
}

Measured memory_bytes: 256