#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdint>
#include <map>
#include <unordered_set>

using namespace std;

template <typename N = std::size_t, typename E = std::size_t>
class DenseGraph
{
public:
    using NodeType = N;
    using EdgeType = E;
    using NeighborListType = std::vector<std::size_t>;

    DenseGraph(bool undirected = true) noexcept
        : undirected_(undirected)
    {
    }

    void push_node(NodeType n) noexcept
    {
        nodes_.push_back(n);
        neighbor_list_.emplace_back();
    }

    void connect(const std::size_t from, const std::size_t to, const EdgeType edge) noexcept
    {
        const std::size_t new_size = std::max(from, to) + 1;

        if (neighbor_list_.size() < new_size)
        {
            neighbor_list_.resize(new_size);
            nodes_.resize(new_size);
        }

        if (undirected_)
        {
            neighbor_list_[from].push_back(to);
            neighbor_list_[to].push_back(from);
            edges_[std::make_pair(std::min(from, to), std::max(from, to))] = edge;
        }
        else
        {
            neighbor_list_[from].push_back(to);
            edges_[std::make_pair(from, to)] = edge;
        }
    }

    void disconnect(const std::size_t from, const std::size_t to) noexcept
    {
        if (undirected_)
        {
            neighbor_list_[from].erase(std::find(neighbor_list_[from].begin(), neighbor_list_[from].end(), to));
            neighbor_list_[to].erase(std::find(neighbor_list_[to].begin(), neighbor_list_[to].end(), from));
            edges_.erase(std::make_pair(std::min(from, to), std::max(from, to)));
        }
        else
        {
            neighbor_list_[from].erase(std::find(neighbor_list_[from].begin(), neighbor_list_[from].end(), to));
            edges_.erase(std::make_pair(from, to));
        }
    }

    const NodeType& node(const std::size_t index) const noexcept
    {
        return nodes_.at(index);
    }

    const EdgeType& edge(const std::size_t from, const std::size_t to) const noexcept
    {
        if (undirected_)
        {
            auto key = std::make_pair(std::min(from, to), std::max(from, to));
            return edges_.at(key);
        }
        else
        {
            auto key = std::make_pair(from, to);
            return edges_.at(key);
        }
    }

    const NeighborListType&
    neighbor(const std::size_t index) const noexcept
    {
        return neighbor_list_[index];
    }

    std::size_t size() const noexcept
    {
        return neighbor_list_.size();
    }

private:
    using EdgeMap = std::map<std::pair<std::size_t, std::size_t>, EdgeType>;

    std::vector<NeighborListType> neighbor_list_;

    EdgeMap edges_;

    std::vector<NodeType> nodes_;

    bool undirected_;
};

template <typename GraphType>
std::vector<std::vector<int>> warshall_floyd(const GraphType& graph)
{
    const std::size_t n = graph.size();

    std::vector<std::vector<int>> distance(n, std::vector<int>(n, std::numeric_limits<int>::max() / 3));

    for (int i = 0; i < n; i++)
    {
        distance[i][i] = 0;
        for (auto j : graph.neighbor(i))
        {
            distance[i][j] = graph.edge(i, j);
        }
    }

    for (std::size_t k = 0; k < n; k++)
    {
        for (std::size_t i = 0; i < n; i++)
        {
            for (std::size_t j = 0; j < n; j++)
            {
                distance[i][j] = std::min(distance[i][j], distance[i][k] + distance[k][j]);
            }
        }
    }
    return distance;
}

int main()
{
    int h, w;
    cin >> h >> w;
    DenseGraph<int, int> graph(false);
    for (int i = 0; i < 10; i++) {
        graph.push_node(i);
    }

    for (int i = 0; i <= 9; i++)
    {
        for (int j = 0; j <= 9; j++)
        {
            int v;
            cin >> v;
            graph.connect(i, j, v);
        }
    }

    auto dist = warshall_floyd(graph);
    
    vector<int> accum(10, 0);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int a;
            cin >> a;
            if (a != -1) {
                accum[a]++;
            }
        }
    }

    int ans = 0;
    for (int i = 0; i <= 9; i++) {
        if (i != 1) {
            ans += dist[i][1] * accum[i];
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 764