#include <iostream>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>

#include <queue>
#include <utility>
#include <vector>
#include <map>

template<class Tdist, class Tnode>
class djikstra {
private:
    class path {
    public:
        Tdist cost;
        unsigned int src;
        unsigned int dst;

        bool operator<(const struct path &obj) const {
            return src < obj.src;
        }
    };

    class node {
    public:
        Tdist dist;
        bool reachable;

        node() {
            reachable = false;
            dist = 0;
        }
    };

    std::vector<path> paths;
    std::vector<node> nodes;
    std::map<Tnode, unsigned int> r_node_name;
    std::map<unsigned int, Tnode> node_name;
public:
    void initialize() {
        nodes.clear();
        paths.clear();
        node_name.clear();
        r_node_name.clear();
    }

    void add_path(Tdist cost, Tnode src, Tnode dst) {
        path temp;
        auto src_i = r_node_name.find(src);
        auto dst_i = r_node_name.find(dst);
        if (src_i == r_node_name.end()) {
            nodes.push_back(node());
            r_node_name.insert(std::make_pair(src, nodes.size() - 1));
            node_name.insert(std::make_pair(nodes.size() - 1, src));
            src_i = r_node_name.find(src);
        }
        if (dst_i == r_node_name.end()) {
            nodes.push_back(node());
            r_node_name.insert(std::make_pair(dst, nodes.size() - 1));
            node_name.insert(std::make_pair(nodes.size() - 1, dst));
            dst_i = r_node_name.find(dst);
        }
        temp.cost = cost;
        temp.src = src_i->second;
        temp.dst = dst_i->second;
        paths.push_back(temp);
    }

    Tdist get_distance(Tnode node) {
        auto node_i = r_node_name.find(node);
        if (node_i == r_node_name.end())
            throw "Invalid node name";
        return nodes[node_i->second].dist;
    }

    void compute(Tnode start, Tdist initial_cost) {
        sort(paths.begin(), paths.end());
        for (auto &n:nodes)
            n.reachable = false;
        auto start_i = r_node_name.find(start);
        if (start_i == r_node_name.end())
            throw "Invalid node name";
        unsigned int start_n = start_i->second;
        nodes[start_n].reachable = true;
        std::priority_queue<std::pair<Tdist, unsigned int>, std::vector<std::pair<Tdist, unsigned int>>, std::greater<std::pair<Tdist, unsigned int>>> open;
        open.push(std::make_pair(initial_cost, start_n));
        while (!open.empty()) {
            auto least = open.top();
            open.pop();
            if (least.first == nodes[least.second].dist) {
                path temp;
                temp.src = least.second;
                auto rg = std::equal_range(paths.begin(), paths.end(), temp);
                for (auto itr = rg.first; itr != rg.second; itr++) {
                    if (nodes[itr->dst].reachable == false || nodes[itr->dst].dist > nodes[itr->src].dist + itr->cost) {
                        nodes[itr->dst].reachable = true;
                        nodes[itr->dst].dist = nodes[itr->src].dist + itr->cost;
                        open.push(std::make_pair(nodes[itr->dst].dist, itr->dst));
                    }
                }
            }
        }
    }
};


using namespace std;

int main(void) {
    int H, W;
    int t;
    cin >> H >> W;
    djikstra<int, int> d;
    for (int i = 0; i <= 9; i++) {
        for (int j = 0; j <= 9; j++) {
            cin >> t;
            d.add_path(t, j, i);
        }
    }
    d.compute(1, 0);
    int cost[10];
    for (int i = 0; i <= 9; i++) {
        cost[i] = d.get_distance(i);
    }
    long long res = 0;
    for (int h = 0; h < H; h++) {
        for (int w = 0; w < W; w++) {
            cin >> t;
            if (t != -1)
                res += cost[t];
        }
    }
    cout << res << endl;
}

Measured memory_bytes: 256