#include <bits/stdc++.h>
using namespace std;
int w[101][101];
typedef pair< int, int > pii;
priority_queue< pii, vector< pii > , greater<pii> > pq;
vector< int > taken;
int a[101];
int n, m;
void process( int u ){
taken[u]= 1;
for( int i= 1; i<= n; ++i ){
if( !taken[i] )
pq.push( { w[u][i], i } );
}
}
int mst(){
taken.assign( n+1, 0 );
process( 1 );
int mst_cost= 0;
while( !pq.empty() ){
pii front= pq.top(); pq.pop();
int u= front.second, w= front.first;
if( !taken[u] ){
//cout << u << " " << w << endl;
mst_cost+= w;
process( u );
}
}
return mst_cost;
}
int main(){
scanf( "%d%d", &n, &m );
for( int i= 0; i< m; ++i )
scanf( "%d", &a[i] );
for( int i= 1; i<= n; ++i )
for( int j= 1; j<= n; ++j )
scanf( "%d", &w[i][j] );
cout << mst() << endl;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IHdbMTAxXVsxMDFdOwoKdHlwZWRlZiBwYWlyPCBpbnQsIGludCA+IHBpaTsKCnByaW9yaXR5X3F1ZXVlPCBwaWksIHZlY3RvcjwgcGlpID4gLCBncmVhdGVyPHBpaT4gPiBwcTsKdmVjdG9yPCBpbnQgPiB0YWtlbjsKaW50IGFbMTAxXTsKCmludCBuLCBtOwoKdm9pZCBwcm9jZXNzKCBpbnQgdSApewoKCXRha2VuW3VdPSAxOwoKCWZvciggaW50IGk9IDE7IGk8PSBuOyArK2kgKXsKCgkJaWYoICF0YWtlbltpXSApCgkJCXBxLnB1c2goIHsgd1t1XVtpXSwgaSB9ICk7Cgl9Cgp9CgppbnQgbXN0KCl7CgoJdGFrZW4uYXNzaWduKCBuKzEsIDAgKTsKCglwcm9jZXNzKCAxICk7CgoJaW50IG1zdF9jb3N0PSAwOwoKCXdoaWxlKCAhcHEuZW1wdHkoKSApewoKCQlwaWkgZnJvbnQ9IHBxLnRvcCgpOyBwcS5wb3AoKTsKCgkJaW50IHU9IGZyb250LnNlY29uZCwgd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