#include <cstdio>
#include <algorithm>
#include <vector>

int main() {
  int H, W;
  scanf("%d %d", &H, &W);
  H *= W;

  int c[10][10];
  for (int i=0; i<10; ++i)
    for (int j=0; j<10; ++j)
      scanf("%d", &c[i][j]);

  for (int k=0; k<10; ++k)
    for (int i=0; i<10; ++i)
      for (int j=0; j<10; ++j)
        c[i][j] = std::min(c[i][j], c[i][k]+c[k][j]);

  int res=0;
  for (int i=0; i<H; ++i) {
    int d;
    scanf("%d", &d);
    if (d == 1 || d == -1)
      continue;

    res += c[d][1];
  }

  printf("%d\n", res);
  return 0;
}
