#include <bits/stdc++.h>
#define int long long
int dsu[30];
int find(int f){
if(dsu[f]==f)return f;
else return dsu[f]=find(dsu[f]);
}
void unionS(int a,int b){
int A = find(a);
int B = find(b);
dsu[A]=B;
}
signed main() {
int n,k;
std::cin >> n >> k;
std::priority_queue<std::pair<int,std::pair<int,int>>> pq;
int curr=n;
int sum=0;
for(int i=1;i<=n;i++){
dsu[i]=i;
for(int j=1;j<=n;j++){
int in;
std::cin >> in;
pq.push({-in,{i,j}});
}
}
while(curr>k){
int v=-pq.top().first;
int a=pq.top().second.first;
int b=pq.top().second.second;
pq.pop();
if(find(a)!=find(b)){
unionS(a,b);
sum+=v;
curr-=1;
}
}
std::cout << sum;
}
/*
5 6
1 2 3 4 5
2 1 3
1 1 3
2 1 5
2 4 4
1 1 1
2 4 4
*/