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



edges = [[] for i in range(10)]



for _from in range(10):

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

    for to in range(10):

        if _from == to:

            continue

        cost = line[to]

        edges[_from].append([to, cost])



cost_to_1 = [10**5] * 10

cost_to_1[1] = 0



for num in range(10):

    if num == 1:

        continue

    min_cost_to = [10**5] * 10

    min_cost_to[num] = 0

    todo = []

    for to, cost in edges[num]:

        if to == num:

            continue

        min_cost_to[to] = cost

        todo.append(to)



    while todo:

        now = todo.pop()

        cost = min_cost_to[now]

        for to, add_cost in edges[now]:

            if to == now:

                continue

            if cost + add_cost < min_cost_to[to]:

                min_cost_to[to] = cost + add_cost

                todo.append(to)

            else:

                continue



    cost_to_1[num] = min_cost_to[1]



ans = 0

for i in range(h):

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

    for num in line:

        if num == -1 or num == 1:

            continue

        ans += cost_to_1[num]



print(ans)

import heapq





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

costs = []

for i in range(10):

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

    costs.append(line)



mini_cost_to_1_from = [10**4] * 10

mini_cost_to_1_from[1] = 0



for num in range(10):

    if num == 1:

        continue

    seen = [False] * 10

    todo = []

    for to in range(10):

        if num == to:

            continue

        cost = costs[num][to]

        heapq.heappush(todo, [cost, to])



    while todo:

        cost, node = heapq.heappop(todo)

        if node == 1:

            mini_cost_to_1_from[num] = cost

            break



        if seen[node]:

            continue



        seen[node] = cost



        for to in range(10):

            if node == to:

                continue

            if seen[to]:

                continue

            add = costs[node][to]

            heapq.heappush(todo, [cost+add, to])



num_count = [0] * 11

for i in range(h):

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

    for num in wall:

        num_count[num] += 1



ans = 0

for num in range(10):

    ans += num_count[num] * mini_cost_to_1_from[num]



print(ans)

h, w = list(map(int, input().split()))
edges = [[] for i in range(10)]
for _from in range(10):
    line = list(map(int, input().split()))
    for to in range(10):
        if _from == to:
            continue
        cost = line[to]
        edges[_from].append([to, cost])
cost_to_1 = [10**5] * 10
cost_to_1[1] = 0
for num in range(10):
    if num == 1:
        continue
    min_cost_to = [10**5] * 10
    min_cost_to[num] = 0
    todo = []
    for to, cost in edges[num]:
        if to == num:
            continue
        min_cost_to[to] = cost
        todo.append(to)
    while todo:
        now = todo.pop()
        cost = min_cost_to[now]
        for to, add_cost in edges[now]:
            if to == now:
                continue
            if cost + add_cost < min_cost_to[to]:
                min_cost_to[to] = cost + add_cost
                todo.append(to)
            else:
                continue
    cost_to_1[num] = min_cost_to[1]
ans = 0
for i in range(h):
    line = list(map(int, input().split()))
    for num in line:
        if num == -1 or num == 1:
            continue
        ans += cost_to_1[num]
print(ans)

import heapq

h, w = list(map(int, input().split()))
costs = []
for i in range(10):
    line = list(map(int, input().split()))
    costs.append(line)
mini_cost_to_1_from = [10**4] * 10
mini_cost_to_1_from[1] = 0
for num in range(10):
    if num == 1:
        continue
    seen = [False] * 10
    todo = []
    for to in range(10):
        if num == to:
            continue
        cost = costs[num][to]
        heapq.heappush(todo, [cost, to])
    while todo:
        cost, node = heapq.heappop(todo)
        if node == 1:
            mini_cost_to_1_from[num] = cost
            break
        if seen[node]:
            continue
        seen[node] = cost
        for to in range(10):
            if node == to:
                continue
            if seen[to]:
                continue
            add = costs[node][to]
            heapq.heappush(todo, [cost + add, to])
num_count = [0] * 11
for i in range(h):
    wall = list(map(int, input().split()))
    for num in wall:
        num_count[num] += 1
ans = 0
for num in range(10):
    ans += num_count[num] * mini_cost_to_1_from[num]
print(ans)

+import heapq + -edges = [[] for i in range(10)] -for _from in range(10): +costs = [] +for i in range(10): -    for to in range(10): -        if _from == to: -            continue -        cost = line[to] -        edges[_from].append([to, cost]) -cost_to_1 = [10**5] * 10 -cost_to_1[1] = 0 +    costs.append(line) +mini_cost_to_1_from = [10**4] * 10 +mini_cost_to_1_from[1] = 0 -    min_cost_to = [10**5] * 10 -    min_cost_to[num] = 0 +    seen = [False] * 10 -    for to, cost in edges[num]: -        if to == num: +    for to in range(10): +        if num == to: -        min_cost_to[to] = cost -        todo.append(to) +        cost = costs[num][to] +        heapq.heappush(todo, [cost, to]) -        now = todo.pop() -        cost = min_cost_to[now] -        for to, add_cost in edges[now]: -            if to == now: +        cost, node = heapq.heappop(todo) +        if node == 1: +            mini_cost_to_1_from[num] = cost +            break +        if seen[node]: +            continue +        seen[node] = cost +        for to in range(10): +            if node == to: -            if cost + add_cost < min_cost_to[to]: -                min_cost_to[to] = cost + add_cost -                todo.append(to) -            else: +            if seen[to]: -    cost_to_1[num] = min_cost_to[1] +            add = costs[node][to] +            heapq.heappush(todo, [cost + add, to]) +num_count = [0] * 11 +for i in range(h): +    wall = list(map(int, input().split())) +    for num in wall: +        num_count[num] += 1 -for i in range(h): -    line = list(map(int, input().split())) -    for num in line: -        if num == -1 or num == 1: -            continue -        ans += cost_to_1[num] +for num in range(10): +    ans += num_count[num] * mini_cost_to_1_from[num]