def main():

    import numpy as np



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

    d = np.zeros((10,10))

    for i in range(10):

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

        d[i] = a

    def WF(b):

        for k in range(10):

            for i in range(10):

                for j in range(10):

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

        return d

    WF(d)

    L = []

    for i in range(h):

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

        L.append(b)

    ans = 0

    for i in range(h):

        for j in range(w):

            if L[i][j] == 1 or L[i][j] == -1:

                continue

            else:

                ans += d[L[i][j]][1]

    print((int(ans)))



if __name__ == '__main__':

	main()
def main():



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

    d = []

    for i in range(10):

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

        d.append(a)

    def WF(d):

        for k in range(10):

            for i in range(10):

                for j in range(10):

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

        return d

    WF(d)

    L = []

    for i in range(h):

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

        L.append(b)

    ans = 0

    for i in range(h):

        for j in range(w):

            if L[i][j] == 1 or L[i][j] == -1:

                continue

            else:

                ans += d[L[i][j]][1]

    print((int(ans)))



if __name__ == '__main__':

	main()
def main():
    import numpy as np

    h, w = list(map(int, input().split()))
    d = np.zeros((10, 10))
    for i in range(10):
        a = list(map(int, input().split()))
        d[i] = a

    def WF(b):
        for k in range(10):
            for i in range(10):
                for j in range(10):
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j])
        return d

    WF(d)
    L = []
    for i in range(h):
        b = list(map(int, input().split()))
        L.append(b)
    ans = 0
    for i in range(h):
        for j in range(w):
            if L[i][j] == 1 or L[i][j] == -1:
                continue
            else:
                ans += d[L[i][j]][1]
    print((int(ans)))


if __name__ == "__main__":
    main()

def main():
    h, w = list(map(int, input().split()))
    d = []
    for i in range(10):
        a = list(map(int, input().split()))
        d.append(a)

    def WF(d):
        for k in range(10):
            for i in range(10):
                for j in range(10):
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j])
        return d

    WF(d)
    L = []
    for i in range(h):
        b = list(map(int, input().split()))
        L.append(b)
    ans = 0
    for i in range(h):
        for j in range(w):
            if L[i][j] == 1 or L[i][j] == -1:
                continue
            else:
                ans += d[L[i][j]][1]
    print((int(ans)))


if __name__ == "__main__":
    main()

-    import numpy as np - -    d = np.zeros((10, 10)) +    d = [] -        d[i] = a +        d.append(a) -    def WF(b): +    def WF(d):