import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;


void main()
{
  auto hw = readln.chomp.split.to!(int[]);
  auto c = new int[][](10, 10);
  
  foreach (i; 0..10) {
    c[i] = readln.chomp.split.to!(int[]);
  }

  int[int] cnt;
  foreach (i; 0..hw[0]) {
    auto a = readln.chomp.split.to!(int[]);
    foreach (j; 0..hw[1]) {
      cnt[a[j]]++;
    }
  }

  foreach (k; 0..10) {
    foreach (i; 0..10) {
      foreach (j; 0..10) {
        if (i == j) continue;
        c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
      }
    } 
  }
  int cost;
  foreach (k; cnt.keys) {
    if (k == -1 || k == 1) continue;
    cost += c[k][1] * cnt[k];
  }

  cost.writeln;
}
