import sys

import collections



sys.setrecursionlimit(10**6)

readline = sys.stdin.readline



def dfs(n, used, cost):

    global c, mincost

    for i in range(10):

        if i == 1:

            if cost + c[n][i] < mincost:

                mincost = cost + c[n][i]

                continue

        if i not in used and cost < mincost:

            dfs(i, used+[i], cost+c[n][i])



def main():

    global c, mincost

    H, W = list(map(int, readline().split()))

    c = []

    for _ in range(10):

        c.append(list(map(int, readline().split())))

    wall = []

    wallcount = collections.Counter([])

    for _ in range(H):

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

        wall.append(tmp)

        wallcount += collections.Counter(tmp)

    

    wallitem = list(zip(*list(wallcount.items())))

    ans = 0

    for i, item in enumerate(wallitem[0]):

        if item in [-1, 1]:

            continue

        mincost = c[item][1]

        dfs(item, [item], 0)

        ans += mincost*wallitem[1][i]

    print(ans)



if __name__ == "__main__":

    main()

import sys

import collections



sys.setrecursionlimit(10**6)

readline = sys.stdin.readline





def main():

    H, W = list(map(int, readline().split()))

    c = []

    for _ in range(10):

        c.append(list(map(int, readline().split())))

    wall = []

    wallcount = collections.Counter([])

    for _ in range(H):

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

        wall.append(tmp)

        wallcount += collections.Counter(tmp)

    wallitem = list(zip(*list(wallcount.items())))

    

    for k in range(10):

        for i in range(10):

            for j in range(10):

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

    

    ans = 0

    for item in wallitem[0]:

        if item in [-1, 1]:

            continue

        ans += wallcount[item]*c[item][1]



    print(ans)



if __name__ == "__main__":

    main()

import sys
import collections

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline


def dfs(n, used, cost):
    global c, mincost
    for i in range(10):
        if i == 1:
            if cost + c[n][i] < mincost:
                mincost = cost + c[n][i]
                continue
        if i not in used and cost < mincost:
            dfs(i, used + [i], cost + c[n][i])


def main():
    global c, mincost
    H, W = list(map(int, readline().split()))
    c = []
    for _ in range(10):
        c.append(list(map(int, readline().split())))
    wall = []
    wallcount = collections.Counter([])
    for _ in range(H):
        tmp = list(map(int, input().split()))
        wall.append(tmp)
        wallcount += collections.Counter(tmp)
    wallitem = list(zip(*list(wallcount.items())))
    ans = 0
    for i, item in enumerate(wallitem[0]):
        if item in [-1, 1]:
            continue
        mincost = c[item][1]
        dfs(item, [item], 0)
        ans += mincost * wallitem[1][i]
    print(ans)


if __name__ == "__main__":
    main()

import sys
import collections

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline


def main():
    H, W = list(map(int, readline().split()))
    c = []
    for _ in range(10):
        c.append(list(map(int, readline().split())))
    wall = []
    wallcount = collections.Counter([])
    for _ in range(H):
        tmp = list(map(int, input().split()))
        wall.append(tmp)
        wallcount += collections.Counter(tmp)
    wallitem = list(zip(*list(wallcount.items())))
    for k in range(10):
        for i in range(10):
            for j in range(10):
                c[i][j] = min(c[i][k] + c[k][j], c[i][j])
    ans = 0
    for item in wallitem[0]:
        if item in [-1, 1]:
            continue
        ans += wallcount[item] * c[item][1]
    print(ans)


if __name__ == "__main__":
    main()

-def dfs(n, used, cost): -    global c, mincost -    for i in range(10): -        if i == 1: -            if cost + c[n][i] < mincost: -                mincost = cost + c[n][i] -                continue -        if i not in used and cost < mincost: -            dfs(i, used + [i], cost + c[n][i]) - - -    global c, mincost +    for k in range(10): +        for i in range(10): +            for j in range(10): +                c[i][j] = min(c[i][k] + c[k][j], c[i][j]) -    for i, item in enumerate(wallitem[0]): +    for item in wallitem[0]: -        mincost = c[item][1] -        dfs(item, [item], 0) -        ans += mincost * wallitem[1][i] +        ans += wallcount[item] * c[item][1]