#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;

}