def warshall_floyd(cost):

    # cost[i][j] : iからjへの最短距離

    for k in range(10):

        for i in range(10):

            for j in range(10):

                cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])

    return cost





h, w = list(map(int, input().split()))

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

a = tuple(tuple(map(int, input().split())) for _ in range(h))



cost = warshall_floyd(cost)



ans = 0

for r in range(h):

    for c in range(w):

        frm = a[r][c]

        if frm == -1:

            continue

        ans += cost[frm][1]

print(ans)

def main():

    h, w = list(map(int, input().split()))

    c = [[int(x) for x in input().split()] for _ in range(10)]

    a = [[int(x) for x in input().split()] for _ in range(h)]



    for k in range(10):

        for i in range(10):

            for j in range(10):

                c[i][j] = min(c[i][j], c[i][k] + c[k][j])



    ret = 0

    for row in a:

        for cell in row:

            if ~cell:

                ret += c[cell][1]



    print(ret)





if __name__ == '__main__':

    main()

   
def warshall_floyd(cost):
    # cost[i][j] : iからjへの最短距離
    for k in range(10):
        for i in range(10):
            for j in range(10):
                cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
    return cost


h, w = list(map(int, input().split()))
cost = [list(map(int, input().split())) for _ in range(10)]
a = tuple(tuple(map(int, input().split())) for _ in range(h))
cost = warshall_floyd(cost)
ans = 0
for r in range(h):
    for c in range(w):
        frm = a[r][c]
        if frm == -1:
            continue
        ans += cost[frm][1]
print(ans)

def main():
    h, w = list(map(int, input().split()))
    c = [[int(x) for x in input().split()] for _ in range(10)]
    a = [[int(x) for x in input().split()] for _ in range(h)]
    for k in range(10):
        for i in range(10):
            for j in range(10):
                c[i][j] = min(c[i][j], c[i][k] + c[k][j])
    ret = 0
    for row in a:
        for cell in row:
            if ~cell:
                ret += c[cell][1]
    print(ret)


if __name__ == "__main__":
    main()

-def warshall_floyd(cost): -    # cost[i][j] : iからjへの最短距離 +def main(): +    h, w = list(map(int, input().split())) +    c = [[int(x) for x in input().split()] for _ in range(10)] +    a = [[int(x) for x in input().split()] for _ in range(h)] -                cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]) -    return cost +                c[i][j] = min(c[i][j], c[i][k] + c[k][j]) +    ret = 0 +    for row in a: +        for cell in row: +            if ~cell: +                ret += c[cell][1] +    print(ret) -h, w = list(map(int, input().split())) -cost = [list(map(int, input().split())) for _ in range(10)] -a = tuple(tuple(map(int, input().split())) for _ in range(h)) -cost = warshall_floyd(cost) -ans = 0 -for r in range(h): -    for c in range(w): -        frm = a[r][c] -        if frm == -1: -            continue -        ans += cost[frm][1] -print(ans) +if __name__ == "__main__": +    main()