import sys

input = sys.stdin.readline



INF = float("inf")





def f(c, pos, unused, sum_c):

    if pos == 1:

        return sum_c

    ret = INF

    l = unused[:]

    l.remove(pos)

    for next_n in l:

        res = f(c, next_n, l, sum_c + c[pos][next_n])

        if res < ret:

            ret = res

    return ret





def main():

    H, W = list(map(int, input().split()))

    c = [None] * 10

    for i in range(10):

        c[i] = list(map(int, input().split()))

    A = [None] * H

    for i in range(H):

        A[i] = list(map(int, input().split()))



    c_min = [0] * 10

    for i in range(10):

        c_min[i] = f(c, i, list(range(10)), 0)



    ans = 0

    for h in range(H):

        for w in range(W):

            if A[h][w] != -1:

                ans += c_min[A[h][w]]

    print(ans)





if __name__ == "__main__":

    main()

import sys

input = sys.stdin.readline



INF = float("inf")





def warshall_floyd(n, d):

    for k in range(n):

        for i in range(n):

            d_ik = d[i][k]

            if d_ik == INF:

                continue

            for j in range(n):

                if d[i][j] > d_ik + d[k][j]:

                    d[i][j] = d_ik + d[k][j]





def main():

    H, W = list(map(int, input().split()))

    c = [None] * 10

    for i in range(10):

        c[i] = list(map(int, input().split()))

    A = [None] * H

    for i in range(H):

        A[i] = list(map(int, input().split()))



    warshall_floyd(10, c)



    ans = 0

    for h in range(H):

        for w in range(W):

            if A[h][w] != -1:

                ans += c[A[h][w]][1]

    print(ans)





if __name__ == "__main__":

    main()

import sys

input = sys.stdin.readline
INF = float("inf")


def f(c, pos, unused, sum_c):
    if pos == 1:
        return sum_c
    ret = INF
    l = unused[:]
    l.remove(pos)
    for next_n in l:
        res = f(c, next_n, l, sum_c + c[pos][next_n])
        if res < ret:
            ret = res
    return ret


def main():
    H, W = list(map(int, input().split()))
    c = [None] * 10
    for i in range(10):
        c[i] = list(map(int, input().split()))
    A = [None] * H
    for i in range(H):
        A[i] = list(map(int, input().split()))
    c_min = [0] * 10
    for i in range(10):
        c_min[i] = f(c, i, list(range(10)), 0)
    ans = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] != -1:
                ans += c_min[A[h][w]]
    print(ans)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline
INF = float("inf")


def warshall_floyd(n, d):
    for k in range(n):
        for i in range(n):
            d_ik = d[i][k]
            if d_ik == INF:
                continue
            for j in range(n):
                if d[i][j] > d_ik + d[k][j]:
                    d[i][j] = d_ik + d[k][j]


def main():
    H, W = list(map(int, input().split()))
    c = [None] * 10
    for i in range(10):
        c[i] = list(map(int, input().split()))
    A = [None] * H
    for i in range(H):
        A[i] = list(map(int, input().split()))
    warshall_floyd(10, c)
    ans = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] != -1:
                ans += c[A[h][w]][1]
    print(ans)


if __name__ == "__main__":
    main()

-def f(c, pos, unused, sum_c): -    if pos == 1: -        return sum_c -    ret = INF -    l = unused[:] -    l.remove(pos) -    for next_n in l: -        res = f(c, next_n, l, sum_c + c[pos][next_n]) -        if res < ret: -            ret = res -    return ret +def warshall_floyd(n, d): +    for k in range(n): +        for i in range(n): +            d_ik = d[i][k] +            if d_ik == INF: +                continue +            for j in range(n): +                if d[i][j] > d_ik + d[k][j]: +                    d[i][j] = d_ik + d[k][j] -    c_min = [0] * 10 -    for i in range(10): -        c_min[i] = f(c, i, list(range(10)), 0) +    warshall_floyd(10, c) -                ans += c_min[A[h][w]] +                ans += c[A[h][w]][1]