void main()
{
    long[] tmp = readln.split.to!(long[]);
    long h = tmp[0], w = tmp[1];
    long[][] c = new long[][](10);
    foreach (i; 0 .. 10)
    {
        c[i] = readln.split.to!(long[]);
    }

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

    long[10] cnts;
    foreach (i; 0 .. h)
    {
        long[] a = readln.split.to!(long[]);
        foreach (x; a)
        {
            if (x == -1) continue;
            ++cnts[x];
        }
    }

    long mp;
    foreach (i; 0 .. 10)
    {
        mp += cnts[i] * c[i][1];
    }

    mp.writeln;
}

import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;