import std.stdio;
import std.string;
import std.conv;
import std.bigint;
import std.typecons;
import std.algorithm;
import std.array;
 
void main() {
    auto tmp = readln.split.to!(int[]);
    auto W = tmp[1], H = tmp[0];
    int[][] cost;
    foreach (i; 0..10) {
        tmp = readln.split.to!(int[]);
        cost ~= tmp;
    }
    foreach (k; 0..10) {
        foreach (i; 0..10) {
            foreach (j; 0..10) {
                cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
            }
        }
    }
    ulong res = 0;
    foreach (i; 0..H) {
        tmp = readln.split.to!(int[]);
        foreach (j; 0..W) {
            auto a = tmp[j];
            if (a == -1) continue;
            res += cost[a][1];
        }
    }
    writeln(res);
}
