def calc_mp(C, candidates, n, ans):

    for i in range(len(candidates)):

        candidates2 = [c for c in candidates]

        c = candidates[i]

        del candidates2[i]

        a = C[n][c]

        if c != 1:

            a += calc_mp(C, candidates2, c, ans)

        ans = min(a, ans)

    return ans



def main():

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

    C = [0]*10

    for i in range(10):

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

    A = [0]*H

    for i in range(H):

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

    # print(H, W, C, A)



    MPs = [0]*10

    for i in range(10):

        if i != 1:

            candidates = [j for j in range(10)]

            del(candidates[i])

            MPs[i] = calc_mp(C, candidates, i, 1000)

    # print(MPs)

    ans = 0

    for h in range(H):

        for w in range(W):

            a = A[h][w]

            if a != -1:

                ans += MPs[a]

    print(ans)



if __name__ == '__main__':

    main()

def calc_mp(C, d):

    for k in range(10):

        for i in range(10):

            for j in range(10):

                if k == 0:

                    d[i][j][k] = min(C[i][j], C[i][k] + C[k][j])

                else:

                    d[i][j][k] = min(d[i][j][k-1], d[i][k][k-1] + d[k][j][k-1])



def main():

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

    C = [0]*10

    for i in range(10):

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

    A = [0]*H

    for i in range(H):

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

    # print(H, W, C, A)



    d = [[[0 for i in range(10)] for i in range(10)] for i in range(10)]

    calc_mp(C, d)



    # print(d)

    ans = 0

    for h in range(H):

        for w in range(W):

            a = A[h][w]

            if a != -1 and a != 1:

                ans += d[a][1][9]

    print(ans)



if __name__ == '__main__':

    main()

def calc_mp(C, candidates, n, ans):
    for i in range(len(candidates)):
        candidates2 = [c for c in candidates]
        c = candidates[i]
        del candidates2[i]
        a = C[n][c]
        if c != 1:
            a += calc_mp(C, candidates2, c, ans)
        ans = min(a, ans)
    return ans


def main():
    H, W = list(map(int, input().split()))
    C = [0] * 10
    for i in range(10):
        C[i] = list(map(int, input().split()))
    A = [0] * H
    for i in range(H):
        A[i] = list(map(int, input().split()))
    # print(H, W, C, A)
    MPs = [0] * 10
    for i in range(10):
        if i != 1:
            candidates = [j for j in range(10)]
            del candidates[i]
            MPs[i] = calc_mp(C, candidates, i, 1000)
    # print(MPs)
    ans = 0
    for h in range(H):
        for w in range(W):
            a = A[h][w]
            if a != -1:
                ans += MPs[a]
    print(ans)


if __name__ == "__main__":
    main()

def calc_mp(C, d):
    for k in range(10):
        for i in range(10):
            for j in range(10):
                if k == 0:
                    d[i][j][k] = min(C[i][j], C[i][k] + C[k][j])
                else:
                    d[i][j][k] = min(d[i][j][k - 1], d[i][k][k - 1] + d[k][j][k - 1])


def main():
    H, W = list(map(int, input().split()))
    C = [0] * 10
    for i in range(10):
        C[i] = list(map(int, input().split()))
    A = [0] * H
    for i in range(H):
        A[i] = list(map(int, input().split()))
    # print(H, W, C, A)
    d = [[[0 for i in range(10)] for i in range(10)] for i in range(10)]
    calc_mp(C, d)
    # print(d)
    ans = 0
    for h in range(H):
        for w in range(W):
            a = A[h][w]
            if a != -1 and a != 1:
                ans += d[a][1][9]
    print(ans)


if __name__ == "__main__":
    main()

-def calc_mp(C, candidates, n, ans): -    for i in range(len(candidates)): -        candidates2 = [c for c in candidates] -        c = candidates[i] -        del candidates2[i] -        a = C[n][c] -        if c != 1: -            a += calc_mp(C, candidates2, c, ans) -        ans = min(a, ans) -    return ans +def calc_mp(C, d): +    for k in range(10): +        for i in range(10): +            for j in range(10): +                if k == 0: +                    d[i][j][k] = min(C[i][j], C[i][k] + C[k][j]) +                else: +                    d[i][j][k] = min(d[i][j][k - 1], d[i][k][k - 1] + d[k][j][k - 1]) -    MPs = [0] * 10 -    for i in range(10): -        if i != 1: -            candidates = [j for j in range(10)] -            del candidates[i] -            MPs[i] = calc_mp(C, candidates, i, 1000) -    # print(MPs) +    d = [[[0 for i in range(10)] for i in range(10)] for i in range(10)] +    calc_mp(C, d) +    # print(d) -            if a != -1: -                ans += MPs[a] +            if a != -1 and a != 1: +                ans += d[a][1][9]