#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

struct node
{
    int mp;
    int n;
    vector<int> path;
    node(int tmp, int tn, vector<int>tp)
    {
        n = tn;
        mp = tmp;
        path = tp;
        path.push_back(tn);
    }
    friend bool operator < (node A, node B)
    {
        return A.mp > B.mp;
    }
};

vector<int> vec_empty;
int h, w;
int spend[10][10];
int pic[205][205];
int save[10];
int res;

int bfs(int ori)
{
    int j;
    bool chkre[10];
    priority_queue<node> que, q_empty;

    memset(chkre, 0, sizeof(chkre));
    que.push(node(0, ori, vec_empty));
    while(que.size())
    {
        node start = que.top();
        que.pop();
        if(start.n == 1)
        {
            res += start.mp;
            //printf("\nres = %d add = %d\n\n", res, start.mp);
            //printf("ori=%d the path:\n", ori);
            //vector<int>::iterator si;
            //for(si = start.path.begin();si != start.path.end();si++) printf("%d\n", *si);
            return 0;
        }
        if(chkre[start.n] == 1) continue;

        for(j = 0;j <= 9;j++)
        {
            if(chkre[j] == 1) continue;
            if(start.n == j) continue;

            //printf("convert %d to %d mp=%d\n", start.n, j, start.mp + spend[start.n][j]);

            que.push(node(start.mp + spend[start.n][j], j, start.path));
        }
    }
    return 0;
}

int main()
{
    while(scanf("%d%d", &h, &w) != EOF)
    {
        int hh,ww;

        res = 0;
        memset(save, -1, sizeof(save));

        for(hh = 0;hh < 10;hh++)
        {
            for(ww = 0;ww < 10;ww++)
            {
                scanf("%d", &spend[hh][ww]);
            }
        }

        for(hh = 0;hh < h;hh++)
        {
            for(ww = 0;ww < w;ww++)
            {
                scanf("%d", &pic[hh][ww]);
                if(pic[hh][ww] != -1 && pic[hh][ww] != 1)
                {
                    if(save[pic[hh][ww]] > 0) res += save[pic[hh][ww]];
                    else bfs(pic[hh][ww]);
                }
            }
        }

        printf("%d\n", res);
    }
    return 0;
}
