if __name__=="__main__":

    sum = 0

    x,y = list(map(int,input().split()))

    c = [list(map(int,input().split())) for n in range(10)]

    for k in range(10):

        for i in range(10):

            for j in range(10):

                if c[i][j]>c[i][k]+c[k][j]:

                    c[i][j] = c[i][k]+c[k][j]



    m = [list(map(int,input().split())) for n in range(int(x))]

    for i in range(x):

        for j in range(y):

            if m[i][j]!=-1:

                sum+=int(c[m[i][j]][1])



    print(sum)





if __name__=="__main__":

    sum = 0

    x,y = list(map(int,input().split()))

    c = [list(map(int,input().split())) for n in range(10)]

    for k in range(10):

        for i in range(10):

            for j in range(10):

                if c[i][j]>c[i][k]+c[k][j]:

                    c[i][j] = c[i][k]+c[k][j]



    for i in range(x):

        a = list(map(int,input().split()))

        for j in a:

            if j!=-1:

                sum+=c[j][1]



    print(sum)





if __name__ == "__main__":
    sum = 0
    x, y = list(map(int, input().split()))
    c = [list(map(int, input().split())) for n in range(10)]
    for k in range(10):
        for i in range(10):
            for j in range(10):
                if c[i][j] > c[i][k] + c[k][j]:
                    c[i][j] = c[i][k] + c[k][j]
    m = [list(map(int, input().split())) for n in range(int(x))]
    for i in range(x):
        for j in range(y):
            if m[i][j] != -1:
                sum += int(c[m[i][j]][1])
    print(sum)

if __name__ == "__main__":
    sum = 0
    x, y = list(map(int, input().split()))
    c = [list(map(int, input().split())) for n in range(10)]
    for k in range(10):
        for i in range(10):
            for j in range(10):
                if c[i][j] > c[i][k] + c[k][j]:
                    c[i][j] = c[i][k] + c[k][j]
    for i in range(x):
        a = list(map(int, input().split()))
        for j in a:
            if j != -1:
                sum += c[j][1]
    print(sum)

-    m = [list(map(int, input().split())) for n in range(int(x))] -        for j in range(y): -            if m[i][j] != -1: -                sum += int(c[m[i][j]][1]) +        a = list(map(int, input().split())) +        for j in a: +            if j != -1: +                sum += c[j][1]