def dijkstra(graph,src):

    if graph is None:

        return None

    nodes = [i for i in range(len(graph))]

    visited=[]

    if src in nodes:

        visited.append(src)

        nodes.remove(src)

    else:

        return None

    distance={src:0}

    for i in nodes:

        distance[i]=int(graph[src][i])

    path={src:{src:[]}}

    k=pre=src

    while nodes:

        mid_distance=float('inf')

        for v in visited:

            for d in nodes:

                new_distance = int(graph[src][v])+int(graph[v][d])

                if new_distance < mid_distance:

                    mid_distance=new_distance

                    graph[src][d]=str(new_distance)

                    k=d

                    pre=v

        distance[k]=mid_distance

        path[src][k]=[i for i in path[src][pre]]

        path[src][k].append(k)

        visited.append(k)

        nodes.remove(k)

    return distance



if __name__=="__main__":

    sum = 0

    s = input().split(" ")

    x, y = s[0], s[1]

    c = [n for n in range(0,10)]

    d = []

    matrix =[i for i in range(0,int(x))]*int(y)

    c[0] = list(input().split(" "))

    c[1] = list(input().split(" "))

    c[2] = list(input().split(" "))

    c[3] = list(input().split(" "))

    c[4] = list(input().split(" "))

    c[5] = list(input().split(" "))

    c[6] = list(input().split(" "))

    c[7] = list(input().split(" "))

    c[8] = list(input().split(" "))

    c[9] = list(input().split(" "))

    for m in range(0,10):

        d.append(dijkstra(c,m))

    for i in range(0,int(x)):

        matrix[i] = input().split(" ")

        for j in range(0,int(y)):

            n = int(matrix[i][j])

            if n==-1:

                continue

            else:

                sum = sum + int(d[n][1])

    print(sum)







if __name__=="__main__":

    sum = 0

    x,y = list(map(int,input().split()))

    c = [list(map(int,input().split())) for n in range(10)]

    for k in range(10):

        for i in range(10):

            for j in range(10):

                if c[i][j]>c[i][k]+c[k][j]:

                    c[i][j] = c[i][k]+c[k][j]



    m = [list(map(int,input().split())) for n in range(int(x))]

    for i in range(x):

        for j in range(y):

            if m[i][j]!=-1:

                sum+=int(c[m[i][j]][1])



    print(sum)





def dijkstra(graph, src):
    if graph is None:
        return None
    nodes = [i for i in range(len(graph))]
    visited = []
    if src in nodes:
        visited.append(src)
        nodes.remove(src)
    else:
        return None
    distance = {src: 0}
    for i in nodes:
        distance[i] = int(graph[src][i])
    path = {src: {src: []}}
    k = pre = src
    while nodes:
        mid_distance = float("inf")
        for v in visited:
            for d in nodes:
                new_distance = int(graph[src][v]) + int(graph[v][d])
                if new_distance < mid_distance:
                    mid_distance = new_distance
                    graph[src][d] = str(new_distance)
                    k = d
                    pre = v
        distance[k] = mid_distance
        path[src][k] = [i for i in path[src][pre]]
        path[src][k].append(k)
        visited.append(k)
        nodes.remove(k)
    return distance


if __name__ == "__main__":
    sum = 0
    s = input().split(" ")
    x, y = s[0], s[1]
    c = [n for n in range(0, 10)]
    d = []
    matrix = [i for i in range(0, int(x))] * int(y)
    c[0] = list(input().split(" "))
    c[1] = list(input().split(" "))
    c[2] = list(input().split(" "))
    c[3] = list(input().split(" "))
    c[4] = list(input().split(" "))
    c[5] = list(input().split(" "))
    c[6] = list(input().split(" "))
    c[7] = list(input().split(" "))
    c[8] = list(input().split(" "))
    c[9] = list(input().split(" "))
    for m in range(0, 10):
        d.append(dijkstra(c, m))
    for i in range(0, int(x)):
        matrix[i] = input().split(" ")
        for j in range(0, int(y)):
            n = int(matrix[i][j])
            if n == -1:
                continue
            else:
                sum = sum + int(d[n][1])
    print(sum)

if __name__ == "__main__":
    sum = 0
    x, y = list(map(int, input().split()))
    c = [list(map(int, input().split())) for n in range(10)]
    for k in range(10):
        for i in range(10):
            for j in range(10):
                if c[i][j] > c[i][k] + c[k][j]:
                    c[i][j] = c[i][k] + c[k][j]
    m = [list(map(int, input().split())) for n in range(int(x))]
    for i in range(x):
        for j in range(y):
            if m[i][j] != -1:
                sum += int(c[m[i][j]][1])
    print(sum)

-def dijkstra(graph, src): -    if graph is None: -        return None -    nodes = [i for i in range(len(graph))] -    visited = [] -    if src in nodes: -        visited.append(src) -        nodes.remove(src) -    else: -        return None -    distance = {src: 0} -    for i in nodes: -        distance[i] = int(graph[src][i]) -    path = {src: {src: []}} -    k = pre = src -    while nodes: -        mid_distance = float("inf") -        for v in visited: -            for d in nodes: -                new_distance = int(graph[src][v]) + int(graph[v][d]) -                if new_distance < mid_distance: -                    mid_distance = new_distance -                    graph[src][d] = str(new_distance) -                    k = d -                    pre = v -        distance[k] = mid_distance -        path[src][k] = [i for i in path[src][pre]] -        path[src][k].append(k) -        visited.append(k) -        nodes.remove(k) -    return distance - - -    s = input().split(" ") -    x, y = s[0], s[1] -    c = [n for n in range(0, 10)] -    d = [] -    matrix = [i for i in range(0, int(x))] * int(y) -    c[0] = list(input().split(" ")) -    c[1] = list(input().split(" ")) -    c[2] = list(input().split(" ")) -    c[3] = list(input().split(" ")) -    c[4] = list(input().split(" ")) -    c[5] = list(input().split(" ")) -    c[6] = list(input().split(" ")) -    c[7] = list(input().split(" ")) -    c[8] = list(input().split(" ")) -    c[9] = list(input().split(" ")) -    for m in range(0, 10): -        d.append(dijkstra(c, m)) -    for i in range(0, int(x)): -        matrix[i] = input().split(" ") -        for j in range(0, int(y)): -            n = int(matrix[i][j]) -            if n == -1: -                continue -            else: -                sum = sum + int(d[n][1]) +    x, y = list(map(int, input().split())) +    c = [list(map(int, input().split())) for n in range(10)] +    for k in range(10): +        for i in range(10): +            for j in range(10): +                if c[i][j] > c[i][k] + c[k][j]: +                    c[i][j] = c[i][k] + c[k][j] +    m = [list(map(int, input().split())) for n in range(int(x))] +    for i in range(x): +        for j in range(y): +            if m[i][j] != -1: +                sum += int(c[m[i][j]][1])