#include <stdbool.h>
#include <stdio.h>
int c[100];

int length(int i, int j) {
    return c[10 * i + j];
}

typedef struct Node {
    int number;
    bool done;
    int cost;
} Node;

int main() {
    int h, w;
    scanf("%d %d", &h, &w);
    for (int i = 0; i < 100; i++) scanf("%d", c + i);

    int costs[10] = {0};

    for (int j = 0; j < 10; j++) {
        if (j == 1) continue;
        Node nodes[10];
        for (int i = 0; i < 10; i++) {
            Node node = {.number = i, .done = false, .cost = -1};
            nodes[i] = node;
        }

        nodes[j].cost = 0;

        while (true) {
            Node doneNode = {.number = -1, .done = false, .cost = -1};
            for (int i = 0; i < 10; i++) {
                Node node = nodes[i];
                if (node.done || node.cost < 0) {
                    continue;
                }
                if (doneNode.number == -1 || node.cost < doneNode.cost) {
                    doneNode = node;
                }
            }
            if (doneNode.number == -1) {
                break;
            }
            nodes[doneNode.number].done = true;
            for (int i = 0; i < 10; i++) {
                if (doneNode.number == i) continue;
                int cost = doneNode.cost + length(doneNode.number, i);
                if (nodes[i].cost < 0 || cost < nodes[i].cost) {
                    nodes[i].cost = cost;
                }
            }
        }

        costs[j] = nodes[1].cost;
    }

    int ans = 0;
    for (int i = 0; i < h * w; i++) {
        int a;
        scanf("%d", &a);
        if (a != -1) ans += costs[a];
    }
    printf("%d\n", ans);
}
