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

c = [0]*10

for i in range(10):

  c[i] = [int(x) for x in input().split()]



for _ in range(10):

  for i in range(10):

    for j in range(10):

      for k in range(10):

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



ans = 0

for i in range(h):

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

  for j in range(w):

    if a[j] == -1:

      continue

    else:

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



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

c = [0]*10

for i in range(10):

  c[i] = [int(x) for x in input().split()]





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]])



ans = 0

for i in range(h):

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

  for j in range(w):

    if a[j] == -1:

      continue

    else:

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



print(ans)





h, w = list(map(int, input().split()))
c = [0] * 10
for i in range(10):
    c[i] = [int(x) for x in input().split()]
for _ in range(10):
    for i in range(10):
        for j in range(10):
            for k in range(10):
                c[i][j] = min([c[i][j], c[i][k] + c[k][j]])
ans = 0
for i in range(h):
    a = [int(x) for x in input().split()]
    for j in range(w):
        if a[j] == -1:
            continue
        else:
            ans += c[a[j]][1]
print(ans)

h, w = list(map(int, input().split()))
c = [0] * 10
for i in range(10):
    c[i] = [int(x) for x in input().split()]
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]])
ans = 0
for i in range(h):
    a = [int(x) for x in input().split()]
    for j in range(w):
        if a[j] == -1:
            continue
        else:
            ans += c[a[j]][1]
print(ans)

-for _ in range(10): +for k in range(10): -            for k in range(10): -                c[i][j] = min([c[i][j], c[i][k] + c[k][j]]) +            c[i][j] = min([c[i][j], c[i][k] + c[k][j]])