#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<functional>
#include<map>
#include<cstdlib>
#include<complex>
#include<numeric>
#include<set>
#include<deque>

using namespace std;
using Int = long long;
Int INF = 1LL << 60;
const Int MOD = 1000000000 + 7;

template<int Modulo = MOD> struct Mint {
	Int val;
	constexpr Mint(Int v = 0) noexcept : val(v% Modulo) {
		if (val < 0) v += Modulo;
	}
	constexpr int getmod() { return Modulo; }
	constexpr Mint operator - () const noexcept { return val ? Modulo - val : 0; }
	constexpr Mint operator + (const Mint& r) const noexcept { return Mint(*this) += r; }
	constexpr Mint operator - (const Mint& r) const noexcept { return Mint(*this) -= r; }
	constexpr Mint operator * (const Mint& r) const noexcept { return Mint(*this) *= r; }
	constexpr Mint operator / (const Mint& r) const noexcept { return Mint(*this) /= r; }
	constexpr Mint& operator += (const Mint& r) noexcept {
		val += r.val;
		if (val >= Modulo) val -= Modulo;
		return *this;
	}
	constexpr Mint& operator -= (const Mint& r) noexcept {
		val -= r.val;
		if (val < 0) val += Modulo;
		return *this;
	}
	constexpr Mint& operator *= (const Mint& r) noexcept {
		val = val * r.val % Modulo;
		return *this;
	}
	constexpr Mint& operator /= (const Mint& r) noexcept {
		Int a = r.val, b = Modulo, u = 1, v = 0;
		while (b) {
			Int t = a / b;
			a -= t * b; swap(a, b);
			u -= t * v; swap(u, v);
		}
		val = val * u % Modulo;
		if (val < 0) val += Modulo;
		return *this;
	}
	constexpr bool operator == (const Mint& r) const noexcept {
		return this->val == r.val;
	}
	constexpr bool operator != (const Mint& r) const noexcept {
		return this->val != r.val;
	}
	friend ostream& operator << (ostream& os, const Mint<Modulo>& x) noexcept {
		return os << x.val;
	}
	friend istream& operator >> (istream& is, Mint<Modulo>& x) noexcept {
		Int tmp; is >> tmp;
		x = Mint<Modulo>(tmp);
		return is;
	}
	friend constexpr Mint<Modulo> modpow(const Mint<Modulo>& a, Int n) noexcept {
		Mint res(1), tmp = a;
		while (n > 0) {
			if (n & 1) res *= tmp;
			tmp *= tmp;
			n >>= 1;
		}
		return res;
	}
};

using mint = Mint<>;

int main() {

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	Int H, W;
	cin >> H >> W;

	vector<vector<Int>> C(10, vector<Int>(10, 0));
	for (Int i = 0; i < 10; i++) {
		for (Int j = 0; j < 10; j++) {
			cin >> C[i][j];
		}
	}


	vector<Int> mar(10,INF);//ある数字から1に到達するのに必要な最小魔力
	mar[1] = 0;
	Int temp;
	Int temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9;
	for (Int a = 0; a < 10; a++) {
		if (a == 1)continue;
		temp = 0;
		for (Int b = 0; b < 10; b++) {
			if (b == 1)continue;
			temp2 = temp;
			temp2 += C[a][b];
			for (Int c = 0; c < 10; c++) {
				if (c == 1)continue;
				temp3 = temp2;
				temp3 += C[b][c];
				for (Int d = 0; d < 10; d++) {
					if (d == 1)continue;
					temp4 = temp3;
					temp4 += C[c][d];
					for (Int e = 0; e < 10; e++) {
						if (e == 1)continue;
						temp5 = temp4;
						temp5 += C[d][e];
						for (Int f = 0; f < 10; f++) {
							if (f == 1)continue;
							temp6 = temp5;
							temp6 += C[e][f];
							for (Int g = 0; g < 10; g++) {
								if (g == 1)continue;
								temp7 = temp6;
								temp7 += C[f][g];
								for (Int h= 0;h < 10; h++) {
									if (h == 1)continue;
									temp8 = temp7;
									temp8 += C[g][h];
									for (Int i = 0; i < 10; i++) {
										if (i == 1)continue;
										temp9 = temp8;
										temp9 += C[h][i];
										temp9 += C[i][1];
										mar[a] = min(mar[a], temp9);
									}
								}
							}
						}
					}
				}
			}
		}
	}



	Int ans = 0;
	Int tempa;
	for (Int i = 0; i < H; i++) {
		for (Int j = 0; j < W; j++) {
			cin >> tempa;
			if (tempa == -1)continue;
			else {
				ans += mar[tempa];
			}
		}

	}
	cout << ans << "\n";
}

Measured memory_bytes: 256