// Original C++ solution (slower):


#include "bits/stdc++.h"

using namespace std;



#define DEBUG(x) cout<<#x<<": "<<x<<endl;

#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl



typedef long long ll;

#define vi vector<int>

#define vl vector<ll>

#define vii vector< vector<int> >

#define vll vector< vector<ll> >

#define vs vector<string>

#define pii pair<int,int>

#define pis pair<int,string>

#define psi pair<string,int>

#define pll pair<ll,ll>

const int inf = 1000000001;

const ll INF = 1e18 * 4;

#define MOD 1000000007

#define mod 1000000009

#define pi 3.14159265358979323846

#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;

int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };

int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };



int main() {

	int h, w;

	cin >> h >> w;

	vii c(10, vi(10));

	int i, j, k;

	for (i = 0; i < 10; i++) {

		for (j = 0; j < 10; j++) {

			cin >> c[i][j];

		}

	}

	vii hyou = c;

	for (i = 0; i < 10; i++) {

		for (j = 0; j < 10; j++) {

			for (k = 0; k < 10; k++) {

				if (hyou[j][k] > hyou[j][i] + hyou[i][k]) {

					hyou[j][k] = hyou[j][i] + hyou[i][k];

				}

			}

		}

	}

	int ans = 0;

	for (i = 0; i < h; i++) {

		for (j = 0; j < w; j++) {

			int a;

			cin >> a;

			if (a >= 0) {

				ans += hyou[a][1];

			}

		}

	}

	cout << ans << endl;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>



void init(int* h, int* w, int c[][10], int a[][210]) {

  int i, j;

  scanf("%d%d", h, w);

  for (i = 0; i < 10; i++) {

    for (j = 0; j < 10; j++) {

      scanf("%d", &c[i][j]);

    }

  }

  for (i = 0; i < *h; i++) {

    for (j = 0; j < *w; j++) {

      scanf("%d", &a[i][j]);

    }

  }

  return;

}



void warshall_floyd(int c[][10]) {

  int k, i, j;

  for (k = 0; k < 10; k++) {

    for (i = 0; i < 10; i++) {

      for (j = 0; j < 10; j++) {

	if (c[i][j] > c[i][k] + c[k][j]) {

	  c[i][j] = c[i][k] + c[k][j];

	}

      }

    }

  }

  return;

}



void ans(int h, int w, int c[][10], int a[][210]) {

  int i, j;

  int ans = 0;

  for (i = 0; i < h; i++) {

    for (j = 0; j < w; j++) {

      if (a[i][j] != -1) {

	ans += c[a[i][j]][1];

      }

    }

  }

  printf("%d\n", ans);

  return;

}



void solve() {

   int h, w, i, j;

   int c[10][10];

   int a[210][210];

   init(&h, &w, c, a);

   warshall_floyd(c);

   ans(h, w, c, a);

   return;

}



int main() {

  solve();

}



/*

2 4

0 9 9 9 9 9 9 9 9 9

9 0 9 9 9 9 9 9 9 9

9 9 0 9 9 9 9 9 9 9

9 9 9 0 9 9 9 9 9 9

9 9 9 9 0 9 9 9 9 2

9 9 9 9 9 0 9 9 9 9

9 9 9 9 9 9 0 9 9 9

9 9 9 9 9 9 9 0 9 9

9 9 9 9 2 9 9 9 0 9

9 2 9 9 9 9 9 9 9 0

-1 -1 -1 -1

8 1 1 8



5 5

0 999 999 999 999 999 999 999 999 999

999 0 999 999 999 999 999 999 999 999

999 999 0 999 999 999 999 999 999 999

999 999 999 0 999 999 999 999 999 999

999 999 999 999 0 999 999 999 999 999

999 999 999 999 999 0 999 999 999 999

999 999 999 999 999 999 0 999 999 999

999 999 999 999 999 999 999 0 999 999

999 999 999 999 999 999 999 999 0 999

999 999 999 999 999 999 999 999 999 0

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1



3 5

0 4 3 6 2 7 2 5 3 3

4 0 5 3 7 5 3 7 2 7

5 7 0 7 2 9 3 2 9 1

3 6 2 0 2 4 6 4 2 3

3 5 7 4 0 6 9 7 6 7

9 8 5 2 2 0 4 7 6 5

5 4 6 3 2 3 0 5 4 3

3 6 2 3 4 2 4 0 8 9

4 6 5 4 3 5 3 2 0 8

2 1 3 4 5 7 8 6 4 0

3 5 2 6 1

2 5 3 2 1

6 9 2 5 6



*/


// Fastest known solution for this problem: