from heapq import heappush, heappop
inf = 100000
h, w = map(int, input().split())
cost = []
for i in range(10):
    lst = list(map(int, input().split()))
    cost.append(lst)


def dijkstra(start):
    min_cost = [inf] * 10
    min_cost[start] = 0
    q = []
    heappush(q, (0, start))
    while q:
        c, v = heappop(q)
        if v == 1:
            return c
        if c > min_cost[v]:
            continue
        for i, nc in enumerate(cost[v]):
            if c + nc < min_cost[i]:
                min_cost[i] = c + nc
                heappush(q, (c + nc, i))


to_one = [None] * 10
for i in range(10):
    to_one[i] = dijkstra(i)
ans = 0
for i in range(h):
    lst = list(map(int, input().split()))
    for j in lst:
        if j != -1:
            ans += to_one[j]
print(ans)

d18a5c834981a13c72d1e629784f493fe99d6a36
<mask token>


def dijkstra(start):
    min_cost = [inf] * 10
    min_cost[start] = 0
    q = []
    heappush(q, (0, start))
    while q:
        c, v = heappop(q)
        if v == 1:
            return c
        if c > min_cost[v]:
            continue
        for i, nc in enumerate(cost[v]):
            if c + nc < min_cost[i]:
                min_cost[i] = c + nc
                heappush(q, (c + nc, i))


<mask token>

<mask token>
for i in range(10):
    lst = list(map(int, input().split()))
    cost.append(lst)


def dijkstra(start):
    min_cost = [inf] * 10
    min_cost[start] = 0
    q = []
    heappush(q, (0, start))
    while q:
        c, v = heappop(q)
        if v == 1:
            return c
        if c > min_cost[v]:
            continue
        for i, nc in enumerate(cost[v]):
            if c + nc < min_cost[i]:
                min_cost[i] = c + nc
                heappush(q, (c + nc, i))


<mask token>
for i in range(10):
    to_one[i] = dijkstra(i)
<mask token>
for i in range(h):
    lst = list(map(int, input().split()))
    for j in lst:
        if j != -1:
            ans += to_one[j]
print(ans)

<mask token>
inf = 100000
h, w = map(int, input().split())
cost = []
for i in range(10):
    lst = list(map(int, input().split()))
    cost.append(lst)


def dijkstra(start):
    min_cost = [inf] * 10
    min_cost[start] = 0
    q = []
    heappush(q, (0, start))
    while q:
        c, v = heappop(q)
        if v == 1:
            return c
        if c > min_cost[v]:
            continue
        for i, nc in enumerate(cost[v]):
            if c + nc < min_cost[i]:
                min_cost[i] = c + nc
                heappush(q, (c + nc, i))


to_one = [None] * 10
for i in range(10):
    to_one[i] = dijkstra(i)
ans = 0
for i in range(h):
    lst = list(map(int, input().split()))
    for j in lst:
        if j != -1:
            ans += to_one[j]
print(ans)

from heapq import heappush, heappop
inf = 100000
h, w = map(int, input().split())
cost = []
for i in range(10):
    lst = list(map(int, input().split()))
    cost.append(lst)


def dijkstra(start):
    min_cost = [inf] * 10
    min_cost[start] = 0
    q = []
    heappush(q, (0, start))
    while q:
        c, v = heappop(q)
        if v == 1:
            return c
        if c > min_cost[v]:
            continue
        for i, nc in enumerate(cost[v]):
            if c + nc < min_cost[i]:
                min_cost[i] = c + nc
                heappush(q, (c + nc, i))


to_one = [None] * 10
for i in range(10):
    to_one[i] = dijkstra(i)
ans = 0
for i in range(h):
    lst = list(map(int, input().split()))
    for j in lst:
        if j != -1:
            ans += to_one[j]
print(ans)
