from heapq import heappop, heappush

import numpy as np







def dijkstra(graph, n, vst=0):

    distance = [float('inf')] * n

    distance[vst] = 0

    checked_v = [0] * n

    route = [''] * 10

    route[vst] = str(vst)

    dist = [float('inf')] * n

    dist[vst] = 0

    q = []

    heappush(q, (0, vst))

    for _ in range(n):

        while True:

            min_dist, search_v = heappop(q)

            if checked_v[search_v] == 0:

                break

        checked_v[search_v] = 1

        for to_v in range(n):

            if checked_v[to_v] == 0:

                cost = graph[search_v][to_v]

                if dist[search_v] + cost < dist[to_v]:

                    dist[to_v] = dist[search_v] + cost

                    route[to_v] = route[search_v] + str(to_v)

                    heappush(q, (dist[to_v], to_v))

    return dist





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

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

graph = np.transpose(graph)

dist = dijkstra(graph, 10, vst=1)





wall_num = []

for i in range(h):

    wall_num += list(map(int, input().split()))







ret = 0

for num in wall_num:

    if num != -1:

        ret += dist[num]





print(ret)
import numpy as np





def dijkstra(cost_matrix, start_node=0):

    node_num = len(cost_matrix)

    distance = np.full(node_num, np.inf)

    distance[start_node] = 0

    unsearched_nodes = [i for i in range(node_num)]

    while unsearched_nodes:

        min_distance = np.inf

        for each_node in unsearched_nodes:

            if distance[each_node] < min_distance:

                min_distance = distance[each_node]

                target_node = each_node

        unsearched_nodes.remove(target_node)

        each_distance = cost_matrix[target_node]

        for i in range(node_num):

            if each_distance[i] != 0:

                distance[i] = min(distance[i], distance[target_node] + each_distance[i])

    return distance.astype(int)





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

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

graph = np.transpose(graph)

dist = dijkstra(graph, start_node=1)





wall_num = []

for i in range(h):

    wall_num += list(map(int, input().split()))







ret = 0

for num in wall_num:

    if num != -1:

        ret += dist[num]





print(ret)
from heapq import heappop, heappush
import numpy as np


def dijkstra(graph, n, vst=0):
    distance = [float("inf")] * n
    distance[vst] = 0
    checked_v = [0] * n
    route = [""] * 10
    route[vst] = str(vst)
    dist = [float("inf")] * n
    dist[vst] = 0
    q = []
    heappush(q, (0, vst))
    for _ in range(n):
        while True:
            min_dist, search_v = heappop(q)
            if checked_v[search_v] == 0:
                break
        checked_v[search_v] = 1
        for to_v in range(n):
            if checked_v[to_v] == 0:
                cost = graph[search_v][to_v]
                if dist[search_v] + cost < dist[to_v]:
                    dist[to_v] = dist[search_v] + cost
                    route[to_v] = route[search_v] + str(to_v)
                    heappush(q, (dist[to_v], to_v))
    return dist


h, w = list(map(int, input().split()))
graph = np.array([np.array(list(map(int, input().split()))) for _ in range(10)])
graph = np.transpose(graph)
dist = dijkstra(graph, 10, vst=1)
wall_num = []
for i in range(h):
    wall_num += list(map(int, input().split()))
ret = 0
for num in wall_num:
    if num != -1:
        ret += dist[num]
print(ret)

import numpy as np


def dijkstra(cost_matrix, start_node=0):
    node_num = len(cost_matrix)
    distance = np.full(node_num, np.inf)
    distance[start_node] = 0
    unsearched_nodes = [i for i in range(node_num)]
    while unsearched_nodes:
        min_distance = np.inf
        for each_node in unsearched_nodes:
            if distance[each_node] < min_distance:
                min_distance = distance[each_node]
                target_node = each_node
        unsearched_nodes.remove(target_node)
        each_distance = cost_matrix[target_node]
        for i in range(node_num):
            if each_distance[i] != 0:
                distance[i] = min(distance[i], distance[target_node] + each_distance[i])
    return distance.astype(int)


h, w = list(map(int, input().split()))
graph = np.array([np.array(list(map(int, input().split()))) for _ in range(10)])
graph = np.transpose(graph)
dist = dijkstra(graph, start_node=1)
wall_num = []
for i in range(h):
    wall_num += list(map(int, input().split()))
ret = 0
for num in wall_num:
    if num != -1:
        ret += dist[num]
print(ret)

-from heapq import heappop, heappush -def dijkstra(graph, n, vst=0): -    distance = [float("inf")] * n -    distance[vst] = 0 -    checked_v = [0] * n -    route = [""] * 10 -    route[vst] = str(vst) -    dist = [float("inf")] * n -    dist[vst] = 0 -    q = [] -    heappush(q, (0, vst)) -    for _ in range(n): -        while True: -            min_dist, search_v = heappop(q) -            if checked_v[search_v] == 0: -                break -        checked_v[search_v] = 1 -        for to_v in range(n): -            if checked_v[to_v] == 0: -                cost = graph[search_v][to_v] -                if dist[search_v] + cost < dist[to_v]: -                    dist[to_v] = dist[search_v] + cost -                    route[to_v] = route[search_v] + str(to_v) -                    heappush(q, (dist[to_v], to_v)) -    return dist +def dijkstra(cost_matrix, start_node=0): +    node_num = len(cost_matrix) +    distance = np.full(node_num, np.inf) +    distance[start_node] = 0 +    unsearched_nodes = [i for i in range(node_num)] +    while unsearched_nodes: +        min_distance = np.inf +        for each_node in unsearched_nodes: +            if distance[each_node] < min_distance: +                min_distance = distance[each_node] +                target_node = each_node +        unsearched_nodes.remove(target_node) +        each_distance = cost_matrix[target_node] +        for i in range(node_num): +            if each_distance[i] != 0: +                distance[i] = min(distance[i], distance[target_node] + each_distance[i]) +    return distance.astype(int) -dist = dijkstra(graph, 10, vst=1) +dist = dijkstra(graph, start_node=1)