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

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

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





def warshall_floyd(d):

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

    for k in range(n):

        for i in range(n):

            for j in range(n):

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

    return d



n=10

warshall_floyd(c)





ans=0

for i in range(h):

    for j in range(w):

        if a[i][j]==-1:

            continue

        else:

            ans+=c[a[i][j]][1]

print(ans)

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

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

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





def warshall_floyd(d):

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

    for k in range(10):

        for i in range(10):

            for j in range(10):

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

    return d

inf=1<<32

d = [[inf for i in range(10)] for _ in range(10)]

#d[u][v] : 辺uvのコスト(存在しないときはinf)

for i in range(10):

    for j in range(10):

        d[i][j]=c[i][j]

for i in range(10):

    d[i][i] = 0 #自身のところに行くコストは０

x=warshall_floyd(d)

ans=0

for i in range(h):

    for j in range(w):

        if a[i][j]==-1:

            continue

        ans+=x[a[i][j]][1]

print(ans)

h, w = list(map(int, input().split()))
c = [list(map(int, input().split())) for _ in range(10)]
a = [list(map(int, input().split())) for _ in range(h)]


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


n = 10
warshall_floyd(c)
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] == -1:
            continue
        else:
            ans += c[a[i][j]][1]
print(ans)

h, w = list(map(int, input().split()))
c = [list(map(int, input().split())) for _ in range(10)]
a = [list(map(int, input().split())) for _ in range(h)]


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


inf = 1 << 32
d = [[inf for i in range(10)] for _ in range(10)]
# d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(10):
    for j in range(10):
        d[i][j] = c[i][j]
for i in range(10):
    d[i][i] = 0  # 自身のところに行くコストは０
x = warshall_floyd(d)
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] == -1:
            continue
        ans += x[a[i][j]][1]
print(ans)

-    for k in range(n): -        for i in range(n): -            for j in range(n): +    for k in range(10): +        for i in range(10): +            for j in range(10): -n = 10 -warshall_floyd(c) +inf = 1 << 32 +d = [[inf for i in range(10)] for _ in range(10)] +# d[u][v] : 辺uvのコスト(存在しないときはinf) +for i in range(10): +    for j in range(10): +        d[i][j] = c[i][j] +for i in range(10): +    d[i][i] = 0  # 自身のところに行くコストは０ +x = warshall_floyd(d) -        else: -            ans += c[a[i][j]][1] +        ans += x[a[i][j]][1]