stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

//template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

 

/* attention

    long longのシフト演算には気をつけよう

    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも

    sizeは(int)とキャストしよう

    ごちゃごちゃ場合分けを考える前に全探索は考えましたか？

*/



const int inf = 1<<29;



int H, W;

vector<vector<int>> cost(220, vector<int>(220, inf));



void floyd(){

    for(int k=0; k<10; k++){

        for(int i=0; i<10; i++){

            if(cost[i][k] == inf) continue;

            for(int j=0; j<10; j++){

                if(cost[k][j] == inf) continue;



                cost[i][j] = min(cost[i][j], cost[i][k]+cost[k][j]);

            }

        }

    }

}



int main(){

    cin >> H >> W;



    for(int i=0; i<10; i++) for(int j=0; j<10; j++) cin >> cost[i][j];

    

    map<int, int> mp;

    for(int i=0; i<H; i++) for(int j=0; j<W; j++){

        int c; cin >> c;

        mp[c]++;

    }



    floyd();



    ll ans = 0;

    for(int i=0; i<10; i++){

        ans += mp[i]*cost[i][1];

    }



    cout << ans << endl;



}

// Faster C++ solution for the same problem (measured speedup: 1.208872192270611x):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for(int i=(l); i<(r); ++i)

typedef long long ll;

typedef unsigned long long ull;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

 

/* attention

    long longのシフト演算には気をつけよう

    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも

    sizeは(int)とキャストしよう

    ごちゃごちゃ場合分けを考える前に全探索は考えましたか？

    詰まった時に、別の分野の問題として考え直す（これdpでは？、グラフとしてみればいいのでは？）

    多くの問題はパターンマッチだが、パターンに落とし込むまでが難しく、そのための訓練としてわからない問題をあれこれ色々な角度から考察してみるのではないか

    cin.tie(0);

    ios::sync_with_stdio(false);<- これら、printfとかと併用しない方が良さそう



*/



const ll mod = 1e9+7;



void chmod(ll &M){

    if(M >= mod) M %= mod;

    else if(M < 0){

        M += (abs(M)/mod + 1)*mod;

        M %= mod;

    }

}



int getl(int i, int N) { return i==0? N-1:i-1; };

int getr(int i, int N) { return i==N-1? 0:i+1; };



/* <--------------------------------------------> */



const int inf=1<<29;



vector<vector<int>> d(10, vector<int>(10, inf));



void floyd(){

    rep(k, 0, 10){

        rep(i, 0, 10){

            if(d[i][k]==inf) continue;

            rep(j, 0, 10){

                if(d[j][k]==inf) continue;

                chmin(d[i][j], d[i][k]+d[k][j]);

            }

        }

    }

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);



    int h, w; cin >> h >> w;

    rep(i, 0, 10) rep(j, 0, 10) cin >> d[i][j];

    floyd();



    map<int, int> mp;

    rep(i, 0, h) rep(j, 0, w){

        int a; cin >> a;

        ++mp[a];

    }



    int ans=0;

    rep(i, 0, 10) ans += mp[i]*d[i][1];

    cout << ans << endl;

    

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define rep(i,a,b) for(int i=int(a);i<int(b);++i)

#define SIZE 200005

#define INF 1000000005LL

#define MOD 1000000007



using namespace std;

typedef long long int ll;

typedef pair <int,int> P;



int H,W;

int c[11][11];

int V;

struct edge{int to,cost;};

vector<edge> G[SIZE];

int d[SIZE];



void dijkstra(int s){

	priority_queue<P, vector<P>, greater<P>> que;

	fill(d,d+V,INF);

	d[s] = 0;

	que.push(P(0,s));



	while(!que.empty()){

		P p = que.top();que.pop();

		int v = p.second;

		if(d[v] < p.first)continue;

		for(int i=0;i<G[v].size();i++){

			edge e = G[v][i];

			if(d[e.to] > d[v] + e.cost){

				d[e.to] = d[v] + e.cost;

				que.push(P(d[e.to],e.to));

			}

		}

	}

}



int main()

{

	cin >> H >> W;

	V = 10;

	int C;

	rep(i,0,10) rep(j,0,10){

		cin >> C;

		//Gは逆辺のグラフ

		G[j].push_back((edge){i,C});

	}

	dijkstra(1);

	int a,ans=0;

	rep(i,0,H) rep(j,0,W){

		cin >> a;

		ans += d[a];

	}

	cout << ans << endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 6.314609441570147x):
#include <bits/stdc++.h>

#define rep(i,a,b) for(int i=int(a);i<int(b);++i)

#define SIZE 15

#define INF 1000000005LL

//#define INF 1e18

#define MOD 1000000007



using namespace std;

typedef long long int ll;

typedef pair <int,int> P;



ll d[SIZE][SIZE];

int V;



void warshall_floyd_init(){

  fill(d[0],d[SIZE],INF);

  rep(i,0,SIZE) d[i][i] = 0;

}



//trueなら負の閉路が存在,0_indexed

bool warshall_floyd(){

  rep(k,0,V) rep(i,0,V){

    if(d[i][k]==INF)continue;

    rep(j,0,V){

      if(d[k][j]==INF)continue;

      d[i][j] = min(d[i][j],d[i][k]+d[k][j]);

    }

  }

  rep(i,0,V) if(d[i][i] < 0) return true;

  return false;

}



void add_edge(int a,int b,ll c){

  d[a][b] = c;

}



int h,w;



int main(){

  V=10;

  cin >> h >> w;

  warshall_floyd_init();

  rep(i,0,10){

    rep(j,0,10){

      ll a;

      cin >> a;

      if(i!=j)add_edge(i,j,a);

    }

  }

  warshall_floyd();

  ll ans=0;

  rep(i,0,h) rep(j,0,w){

    int a;

    cin >> a;

    if(a!=1 && a!=-1){

      ans += d[a][1];

    }

  }

  cout << ans << endl;



  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define REP(i,a,b) for(int i=(a);i<(b);i++)

#define RREP(i,a,b) for(int i=(a);i>=(b);i--)

#define pq priotity_queue

#define P pair<int,int>

#define P2 pair<int,P>

#define P3 pair<int,P2>

typedef long long ll; typedef long double ld;

using namespace std;

const int INF=1e9, MOD=1e9+7, around[]={0,1,1,-1,-1,0,-1,1,0,0};

const ll LINF=1e18;

const ld PI=abs(acos(-1));

int h,w,a[210][210],cost[10][10];



int main(){

	cin >> h >> w;

	REP(i,0,10) REP(j,0,10) cin >> cost[i][j];

	REP(i,0,h) REP(j,0,w) cin >> a[i][j];

	

	REP(k,0,10){

		REP(i,0,10){

			REP(j,0,10){

				cost[i][j]=min(cost[i][j], cost[i][k]+cost[k][j]);

			}

		}

	}

	

	int s=0;

	REP(i,0,h){

		REP(j,0,w){

			if(a[i][j]==-1) continue;

			s+=cost[a[i][j]][1];

		}

	}

	

	cout << s << endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.923216758822842x):
#include <stdio.h>

#define REP(i,a,b) for(int i=(a);i<(b);i++)

#define RREP(i,a,b) for(int i=(a);i>=(b);i++)

int h,w,a,li[15][15],s=0;



int min(int a, int b){

	if(a>b) return b;

	else return a;

}



int main(void){

	scanf("%d %d",&h,&w);

	REP(i,0,10) REP(j,0,10) scanf("%d", &li[i][j]);

	REP(k,0,10) REP(i,0,10) REP(j,0,10) li[i][j]=min(li[i][j], li[i][k]+li[k][j]);

	REP(i,0,h) REP(j,0,w){

		scanf("%d",&a);

		if(a==-1) continue;

		else s+=li[a][1];

	}

	

	printf("%d\n",s);

	return 0;

}
// Slower C++ solution:
#include <iostream>



using namespace std;



const int INF = 100000000;

int c[10][10] = {0};

int d[10] = {0};



void bellman_ford(int s) {

    d[s] = 0;

    for (int i = 0; i < 10; i++) {

        for (int j = 0; j < 10; j++) {

            for (int k = 0; k < 10; k++) {

                if (j == k) continue;

                int cost = c[k][j];

                // cout << c[j][k];

                if (d[j] != INF && d[k] > d[j] + cost) {

                    d[k] = d[j] + cost;

                }

            }

        }

    }

}



int main() {

    int H, W;

    int A[201][201] = {0};

    int ans = 0;



    cin >> H >> W;



    for (int i = 0; i < 10; i++) {

        for (int j = 0; j < 10; j++) {

            cin >> c[i][j];

        }

    }

    for (int i = 0; i < 10; i++) {

        d[i] = INF;

    }



    bellman_ford(1);



    for (int i = 0; i < W; i++) {

        for (int j = 0; j < H; j++) {

            cin >> A[i][j];

            if (A[i][j] != -1) {

                ans += d[A[i][j]];

            }

        }

    }



    cout << ans << endl;



    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1547509543283025x):
#include <iostream>



using namespace std;



const int INF = 100000000;

int c[10][10] = {0};

int d[10] = {0};



void bellman_ford(int s) {

    d[s] = 0;

    for (int i = 0; i < 10; i++) {

        for (int j = 0; j < 10; j++) {

            for (int k = 0; k < 10; k++) {

                if (j == k) continue;

                int cost = c[k][j];

                if (d[j] != INF && d[k] > d[j] + cost) {

                    d[k] = d[j] + cost;

                }

            }

        }

    }

}



int main() {

    int H, W;

    int t, ans = 0;



    cin >> H >> W;



    for (int i = 0; i < 10; i++) {

        for (int j = 0; j < 10; j++) {

            cin >> c[i][j];

        }

    }

    for (int i = 0; i < 10; i++) {

        d[i] = INF;

    }



    bellman_ford(1);



    for (int i = 0; i < W; i++) {

        for (int j = 0; j < H; j++) {

            cin >> t;

            if (t != -1) {

                ans += d[t];

            }

        }

    }



    cout << ans << endl;



    return 0;

}
// Slower C++ solution:
#include <iostream>



using namespace std;



const int INF = 100000000;

int c[10][10] = {0};

int d[10] = {0};



void bellman_ford(int s) {

    d[s] = 0;

    for (int i = 0; i < 10; i++) {

        for (int j = 0; j < 10; j++) {

            for (int k = 0; k < 10; k++) {

                if (j == k) continue;

                int cost = c[k][j];

                // cout << c[j][k];

                if (d[j] != INF && d[k] > d[j] + cost) {

                    d[k] = d[j] + cost;

                }

            }

        }

    }

}



int main() {

    int H, W;

    int A[201][201] = {0};

    int ans = 0;



    cin >> H >> W;



    for (int i = 0; i < 10; i++) {

        for (int j = 0; j < 10; j++) {

            cin >> c[i][j];

        }

    }

    for (int i = 0; i < 10; i++) {

        d[i] = INF;

    }



    bellman_ford(1);



    for (int i = 0; i < W; i++) {

        for (int j = 0; j < H; j++) {

            cin >> A[i][j];

            if (A[i][j] != -1) {

                ans += d[A[i][j]];

            }

        }

    }



    cout << ans << endl;



    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1559882565008262x):
#include <iostream>



using namespace std;



int main() {

    int H, W, t, ans = 0;

    int c[10][10];



    cin >> H >> W;



    for (int i = 0; i < 10; i++) {

        for (int j = 0; j < 10; j++) {

            cin >> c[i][j];

        }

    }



    for (int k = 0; k < 10; k++) {

        for (int i = 0; i < 10; i++) {

            for (int j = 0; j < 10; j++) {

                c[i][j] = min(c[i][j], c[i][k] + c[k][j]);

            }

        }

    }

    for (int i = 0; i < W; i++) {

        for (int j = 0; j < H; j++) {

            cin >> t;

            if (t != -1) { ans += c[t][1]; }

        }

    }

    cout << ans << endl;



    return 0;

}
// Slower C++ solution:
#include <iostream>



using namespace std;



const int INF = 100000000;

int c[10][10] = {0};

int d[10] = {0};



void bellman_ford(int s) {

    d[s] = 0;

    for (int i = 0; i < 10; i++) {

        for (int j = 0; j < 10; j++) {

            for (int k = 0; k < 10; k++) {

                if (j == k) continue;

                int cost = c[k][j];

                // cout << c[j][k];

                if (d[j] != INF && d[k] > d[j] + cost) {

                    d[k] = d[j] + cost;

                }

            }

        }

    }

}



int main() {

    int H, W;

    int A[201][201] = {0};

    int ans = 0;



    cin >> H >> W;



    for (int i = 0; i < 10; i++) {

        for (int j = 0; j < 10; j++) {

            cin >> c[i][j];

        }

    }

    for (int i = 0; i < 10; i++) {

        d[i] = INF;

    }



    bellman_ford(1);



    for (int i = 0; i < W; i++) {

        for (int j = 0; j < H; j++) {

            cin >> A[i][j];

            if (A[i][j] != -1) {

                ans += d[A[i][j]];

            }

        }

    }



    cout << ans << endl;



    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1386306263378065x):
#include <iostream>

#include <vector>



using namespace std;



std::vector<std::vector<long long>> warshall_floyd(std::vector<std::vector<long long>> dist) {

  const long long LINF = 1000000000000000018LL;

  int n = (int)dist.size();

  for (int k = 0; k < n; k++) {

    for (int i = 0; i < n; i++) {

      if (dist[i][k] == LINF) {

        continue;

      }

      for (int j = 0; j < n; j++) {

        if (dist[k][j] == LINF) {

          continue;

        }

        dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);

      }

    }

  }

  return dist;

}



int main() {

  int H, W;

  std::cin >> H >> W;



  std::vector<std::vector<long long>> G(10, std::vector<long long>(10));

  for (int y = 0; y < 10; y++) {

    for (int x = 0; x < 10; x++) {

      std::cin >> G[y][x];

    }

  }

  auto dist = warshall_floyd(G);



  int ans = 0;

  for (int y = 0; y < H; y++) {

    for (int x = 0; x < W; x++) {

      int A;

      std::cin >> A;

      if (A != -1) {

        ans += dist[A][1];

      }

    }

  }

  std::cout << ans << "\n";



  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



int n,m;

int d[15][15],maze[205][205];



void Floyd()

{

    for(int k=0;k<10;k++)

        for(int i=0;i<10;i++)

            for(int j=0;j<10;j++)

                d[i][j]=min(d[i][j],d[i][k]+d[k][j]);

}



void input()

{

    for(int i=0;i<10;i++){

        for(int j=0;j<10;j++){

            scanf("%d",&d[i][j]);

        }

    }

    for(int i=1;i<=n;i++){

        for(int j=1;j<=m;j++){

            scanf("%d",&maze[i][j]);

        }

    }

}



void solve()

{

    int sum=0;

    Floyd();

    for(int i=1;i<=n;i++){

        for(int j=1;j<=m;j++){

            if(maze[i][j]!=-1){

                sum+=d[maze[i][j]][1];

            }

        }

    }

    printf("%d\n",sum);

}



int main()

{

    scanf("%d%d",&n,&m);

    input();

    solve();

}


// Faster C++ solution for the same problem (measured speedup: 4.785369151535255x):
#include<cstdio>

#include<algorithm>

using namespace std;

int map[10][10];

int h, w;



void floyd(){

    int i, j, k;

    for(k = 0; k<10; k++){

        for(i = 0; i<10; i++){

            for(j = 0; j<10; j++){

                map[i][j] = min(map[i][j], map[i][k] + map[k][j]);

            }

        }

    }

}



int main(){

    int i, j, a;

    while(~scanf("%d%d",&h, &w)){

        for(i = 0; i<10; i++){

            for(j = 0; j<10; j++){

                scanf("%d",&map[i][j]);

            }

        }

        floyd();

        int ans = 0;

        for(i = 0; i<h; i++){

            for(j = 0; j<w; j++){

                scanf("%d",&a);

                if(a!=-1 && a!=1){

                    ans += map[a][1];

                }

            }

        }

        printf("%d\n", ans);

    }

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

 

using namespace std;

 

#define fi first

#define se second

#define mp make_pair

#define pb push_back

 

typedef long long ll;

 

const ll mod = 1e9+7;

const double pi = acos(-1);



int n,t,j,i,k;

int m, c[12][12];

int f[11],bc[11];

int dp[12][12];





int main(){

	memset(f, 0, sizeof f);

	scanf("%d %d", &n, &m);

	for(i=0;i<10;i++){

		for(j=0;j<10;j++){

			scanf("%d", c[i]+j);

			dp[i][j] = c[i][j];

		}

	}

	for(i=0;i<n;i++){

		for(j=0;j<m;j++){

			scanf("%d", &t);

			if(t>=0)

				f[t]++;

		}

	}

	newload:

	for(i=0;i<10;i++){

		for(j=0;j<10;j++){

			for(k=0;k<10;k++){

				if(dp[i][j] > dp[i][k]+dp[k][j]){

					dp[i][j] = dp[i][k]+dp[k][j];

					goto newload;

				}

			}

		}

	}

	int ans =0;

	for(i=0;i<10;i++)

		ans += f[i]*dp[i][1];

	printf("%d\n", ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.868929022349145x):
#include<cstdio>

#include<algorithm>

using namespace std;

int map[10][10];

int h, w;



void floyd(){

    int i, j, k;

    for(k = 0; k<10; k++){

        for(i = 0; i<10; i++){

            for(j = 0; j<10; j++){

                map[i][j] = min(map[i][j], map[i][k] + map[k][j]);

            }

        }

    }

}



int main(){

    int i, j, a;

    while(~scanf("%d%d",&h, &w)){

        for(i = 0; i<10; i++){

            for(j = 0; j<10; j++){

                scanf("%d",&map[i][j]);

            }

        }

        floyd();

        int ans = 0;

        for(i = 0; i<h; i++){

            for(j = 0; j<w; j++){

                scanf("%d",&a);

                if(a!=-1 && a!=1){

                    ans += map[a][1];

                }

            }

        }

        printf("%d\n", ans);

    }

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



int n,m;

int d[15][15],maze[205][205];



void Floyd()

{

    for(int k=0;k<10;k++)

        for(int i=0;i<10;i++)

            for(int j=0;j<10;j++)

                d[i][j]=min(d[i][j],d[i][k]+d[k][j]);

}



void input()

{

    for(int i=0;i<10;i++){

        for(int j=0;j<10;j++){

            scanf("%d",&d[i][j]);

        }

    }

    for(int i=1;i<=n;i++){

        for(int j=1;j<=m;j++){

            scanf("%d",&maze[i][j]);

        }

    }

}



void solve()

{

    int sum=0;

    Floyd();

    for(int i=1;i<=n;i++){

        for(int j=1;j<=m;j++){

            if(maze[i][j]!=-1){

                sum+=d[maze[i][j]][1];

            }

        }

    }

    printf("%d\n",sum);

}



int main()

{

    scanf("%d%d",&n,&m);

    input();

    solve();

}


// Faster C++ solution for the same problem (measured speedup: 4.761865172306719x):
#include<stdio.h>

#include<algorithm>

 

FILE *in, *out;

 

using namespace std;

 

int v[10][10], n;

 

int main ()

{

 

    //fscanf(stdin, "%d", &n);

    n = 10;

    long long toto = 0;

    int l, c;

    scanf("%d %d", &l, &c);

 

    for(int i = 0; i < n; i++) {

            for(int j = 0; j < n; j++) {

                    fscanf(stdin, "%d", &v[i][j]);

                    if (i == j) {

                            v[i][j] = 0x3f3f3f3f;

                    }

            }

    }

 

    for(int k = 0; k < n; k++) {

            for(int i = 0; i < n; i++) {

                    for(int j = 0; j < n; j++) {

                            v[i][j] = min(v[i][j], (v[i][k] + v[k][j]));

                    }

            }

    }

 

    for(int i = 0; i < n; i++) {

            for(int j = 0; j < n; j++) {

                    if (v[i][j] > 0x3f3f3f3f || i == j) {

                            v[i][j] = 0;

                    }

                    //fprintf(stdout, "%d ", v[i][j]);

            }

            //fprintf(stdout, "\n");

    }



    for(int i = 0; i < l; i++) {

        for(int j = 0; j < c; j++) {

            int x;

            scanf("%d", &x);

            if(x != -1) {

                toto += v[x][1];

            }

        }

    }



    printf("%lld", toto);



    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

 

using namespace std;

 

#define fi first

#define se second

#define mp make_pair

#define pb push_back

 

typedef long long ll;

 

const ll mod = 1e9+7;

const double pi = acos(-1);



int n,t,j,i,k;

int m, c[12][12];

int f[11],bc[11];

int dp[12][12];





int main(){

	memset(f, 0, sizeof f);

	scanf("%d %d", &n, &m);

	for(i=0;i<10;i++){

		for(j=0;j<10;j++){

			scanf("%d", c[i]+j);

			dp[i][j] = c[i][j];

		}

	}

	for(i=0;i<n;i++){

		for(j=0;j<m;j++){

			scanf("%d", &t);

			if(t>=0)

				f[t]++;

		}

	}

	newload:

	for(i=0;i<10;i++){

		for(j=0;j<10;j++){

			for(k=0;k<10;k++){

				if(dp[i][j] > dp[i][k]+dp[k][j]){

					dp[i][j] = dp[i][k]+dp[k][j];

					goto newload;

				}

			}

		}

	}

	int ans =0;

	for(i=0;i<10;i++)

		ans += f[i]*dp[i][1];

	printf("%d\n", ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.845014627663462x):
#include<stdio.h>

#include<algorithm>

 

FILE *in, *out;

 

using namespace std;

 

int v[10][10], n;

 

int main ()

{

 

    //fscanf(stdin, "%d", &n);

    n = 10;

    long long toto = 0;

    int l, c;

    scanf("%d %d", &l, &c);

 

    for(int i = 0; i < n; i++) {

            for(int j = 0; j < n; j++) {

                    fscanf(stdin, "%d", &v[i][j]);

                    if (i == j) {

                            v[i][j] = 0x3f3f3f3f;

                    }

            }

    }

 

    for(int k = 0; k < n; k++) {

            for(int i = 0; i < n; i++) {

                    for(int j = 0; j < n; j++) {

                            v[i][j] = min(v[i][j], (v[i][k] + v[k][j]));

                    }

            }

    }

 

    for(int i = 0; i < n; i++) {

            for(int j = 0; j < n; j++) {

                    if (v[i][j] > 0x3f3f3f3f || i == j) {

                            v[i][j] = 0;

                    }

                    //fprintf(stdout, "%d ", v[i][j]);

            }

            //fprintf(stdout, "\n");

    }



    for(int i = 0; i < l; i++) {

        for(int j = 0; j < c; j++) {

            int x;

            scanf("%d", &x);

            if(x != -1) {

                toto += v[x][1];

            }

        }

    }



    printf("%lld", toto);



    return 0;

}
// Slower C++ solution:
/**

  *  Worg

  */

#include <iostream>

#include <algorithm>



int cost[10][10];



void Optimize() {

  for (int k = 0; k < 10; k++) {

    for (int i = 0; i < 10; i++) {

      for (int j = 0; j < 10; j++) {

        cost[i][j] = std::min(cost[i][j], cost[i][k] + cost[k][j]);

      }

    }

  }

}



int main() {

  int h, w; std::cin >> h >> w;



  for (int i = 0; i < 10; i++) {

    for (int j = 0; j < 10; j++) {

      std::cin >> cost[i][j];

    }

  }



  Optimize();



  int answer = 0;

  for (int i = 0; i < h; i++) {

    for (int j = 0; j < w; j++) {

      int x; std::cin >> x;



      if (x == 1 || x == -1) continue;



      answer += cost[x][1];

    }

  }



  std::cout << answer << '\n';



  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.873277584206775x):
#include<stdio.h>

#include<algorithm>

 

FILE *in, *out;

 

using namespace std;

 

int v[10][10], n;

 

int main ()

{

 

    //fscanf(stdin, "%d", &n);

    n = 10;

    long long toto = 0;

    int l, c;

    scanf("%d %d", &l, &c);

 

    for(int i = 0; i < n; i++) {

            for(int j = 0; j < n; j++) {

                    fscanf(stdin, "%d", &v[i][j]);

                    if (i == j) {

                            v[i][j] = 0x3f3f3f3f;

                    }

            }

    }

 

    for(int k = 0; k < n; k++) {

            for(int i = 0; i < n; i++) {

                    for(int j = 0; j < n; j++) {

                            v[i][j] = min(v[i][j], (v[i][k] + v[k][j]));

                    }

            }

    }

 

    for(int i = 0; i < n; i++) {

            for(int j = 0; j < n; j++) {

                    if (v[i][j] > 0x3f3f3f3f || i == j) {

                            v[i][j] = 0;

                    }

                    //fprintf(stdout, "%d ", v[i][j]);

            }

            //fprintf(stdout, "\n");

    }



    for(int i = 0; i < l; i++) {

        for(int j = 0; j < c; j++) {

            int x;

            scanf("%d", &x);

            if(x != -1) {

                toto += v[x][1];

            }

        }

    }



    printf("%lld", toto);



    return 0;

}