#include <bits/stdc++.h>
using namespace std;

struct init_ { init_(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } init_;

template<class C> using iodef_rng_t = typename enable_if<decltype(declval<C>().begin(), true_type{})::value && !is_same<C, string>::value>::type;
template<class C, class = iodef_rng_t<C>> istream& operator>>(istream& in, C& c) { for (auto& v : c) { in >> v; } return in; }
template<class C, class = iodef_rng_t<C>> ostream& operator<<(ostream& out, const C& c) { int i = 0; for (const auto& v : c) { if (i++) { out << ' '; } out << v; } return out; }
template<size_t N, class T, class R> using when_last = typename enable_if<N == tuple_size<remove_reference_t<T>>::value - 1, R>::type;
template<size_t N, class T, class R> using when_init = typename enable_if<N != tuple_size<remove_reference_t<T>>::value - 1, R>::type;
template<size_t N = 0, class T> auto operator>>(istream& in, T&& t) -> when_last<N, T, istream&> { return in >> get<N>(t); }
template<size_t N = 0, class T> auto operator>>(istream& in, T&& t) -> when_init<N, T, istream&> { return operator>><N + 1>(in >> get<N>(t), forward<T>(t)); }
template<size_t N = 0, class T> auto operator<<(ostream& out, const T& t) -> when_last<N, T, ostream&> { return out << get<N>(t); }
template<size_t N = 0, class T> auto operator<<(ostream& out, const T& t) -> when_init<N, T, ostream&> { return operator<<<N + 1>(out << get<N>(t) << ' ', t); }

#ifdef LOCAL
template<class T> void trace_(const char* s, const T& v) { cerr << s << "=" << v; }
template<class T, class... Ts> void trace_(const char* s, const T& v, const Ts&... vs) { for (; *s != ','; ++s) { cerr << *s; } cerr << "=" << v << ","; trace_(++s, vs...); }
#define TRACE(...) (cerr << "\033[2m\033[33m", trace_(#__VA_ARGS__, __VA_ARGS__), cerr << "\033[0m\n")
#else
#define TRACE(...)
#endif

template<class T> struct Grid {
    vector<T> data;
    const size_t h, w;
    Grid(size_t h, size_t w, T v = T()) : data(h * w, v), h(h), w(w) {}
    T& operator()(size_t i, size_t j) { return data[i * w + j]; }
    friend istream& operator>>(istream& in, Grid& g) {
        for (auto& v : g.data) in >> v;
        return in;
    }
    friend ostream& operator<<(ostream& out, const Grid& g) {
        for (size_t i = 0; i < g.h; ++i) {
            cout << g.data[i * g.w];
            for (size_t j = 1; j < g.w; ++j) {
                out << " " << g.data[i * g.w + j];
            }
            out << "\n";
        }
        return out;
    }
};

template<class V, class W, class F>
void dijkstra(F adj_v_w, V start, vector<W>& dest) {
    using P = pair<W, V>;
    fill(begin(dest), end(dest), numeric_limits<W>::max());
    priority_queue<P, vector<P>, greater<P>> pq;
    dest[start] = 0;
    pq.emplace(dest[start], start);
    while (!pq.empty()) {
        auto p = pq.top(); pq.pop();
        auto wv = p.first; auto v = p.second;
        TRACE(v, wv);
        if (dest[v] < wv) continue;
        for (auto q : adj_v_w(v)) {
            auto u = q.first;
            auto wu = q.second;
            if (dest[u] > dest[v] + wu) {
                dest[u] = dest[v] + wu;
                pq.emplace(dest[u], u);
            }
        }
    }
}

int main() {
    int h, w;
    cin >> h >> w;
    Grid<int> c(10, 10), a(h, w);
    cin >> c >> a;
    auto adj_vw = [&](int i) {
        vector<pair<int, int>> v;
        for (int j = 0; j < 10; j++) {
            v.emplace_back(j, c(j, i));
        }
        return v;
    };
    vector<int> dest(10);
    dijkstra(adj_vw, 1, dest);
    TRACE(dest);
    int64_t ans = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (a(i, j) != -1) {
                ans += dest[a(i, j)];
            }
        }
    }
    cout << ans << '\n';
}


Measured memory_bytes: 384