import std.stdio;
import std.array;
import std.algorithm;
import std.conv;
import std.numeric;
import std.string;

void main() {
  auto l = readln.chomp.split.map!(to!int).array;
  int H = l[0], W = l[1];
  int[][] c = new int[][](10, 10);
  int[][] A = new int[][](H, W);

  foreach(i; 0..10) {
    c[i][] = readln.chomp.split.map!(to!int).array;
  }
  foreach(i; 0..H) {
    A[i][] = readln.chomp.split.map!(to!int).array;
  }

  foreach(k; 0..10) {
    foreach(j; 0..10) {
      foreach(i; 0..10) {
        c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
      }
    }
  }

  int ans;
  foreach(i; 0..H) {
    foreach(j; 0..W) {
      if (A[i][j] != -1 && A[i][j] != 1) {
        ans += c[A[i][j]][1];
      }
    }
  }

  ans.writeln;
}
