#include <bits/stdc++.h>
using namespace std;
int w[101][101];
vector< int > p;
int main(){
int n, k;
scanf( "%d%d", &n, &k );
for( int i= 0, x; i< k; ++i ){
scanf( "%d", &x );
p.push_back(x);
}
for( int i= 1; i<= n; ++i )
for( int j= 1; j<= n; ++j )
scanf( "%d", &w[i][j] );
for( int k= 1; k<= n; ++k )
for( int i= 1; i<= n; ++i )
for( int j= 1; j<= n; ++j )
w[i][j]= min( w[i][j], w[i][k]+w[k][j] );
int mini_cost= 0;
for( int i= 1; i<= n; ++i ){
int min= 1<<30;
//int in;
for( int j= 0; j< p.size(); ++j ){
if( p[j]== i ){
min= 0;
//in= i;
break;
}
if( min > w[i][p[j]] ){
min= w[i][p[j]];
//in= p[j];
}
}
//cout << i << " " << in << " " << min << endl;
mini_cost+= min;
}
cout << mini_cost << endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IHdbMTAxXVsxMDFdOwp2ZWN0b3I8IGludCA+IHA7CgppbnQgbWFpbigpewoKCWludCBuLCBrOwoKCXNjYW5mKCAiJWQlZCIsICZuLCAmayApOwoKCWZvciggaW50IGk9IDAsIHg7IGk8IGs7ICsraSApewoJCXNjYW5mKCAiJWQiLCAmeCApOwoJCXAucHVzaF9iYWNrKHgpOwoJfQoKCWZvciggaW50IGk9IDE7IGk8PSBuOyArK2kgKQoJCWZvciggaW50IGo9IDE7IGo8PSBuOyArK2ogKQoJCQlzY2FuZiggIiVkIiwgJndbaV1bal0gKTsKCglmb3IoIGludCBrPSAxOyBrPD0gbjsgKytrICkKCQlmb3IoIGludCBpPSAxOyBpPD0gbjsgKytpICkKCQkJZm9yKCBpbnQgaj0gMTsgajw9IG47ICsraiApCgkJCQl3W2ldW2pdPSBtaW4oIHdbaV1bal0sIHdbaV1ba10rd1trXVtqXSApOwoKCWludCBtaW5pX2Nvc3Q9IDA7CgoJZm9yKCBpbnQgaT0gMTsgaTw9IG47ICsra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