#include<bits/stdc++.h>
#define int long long
#define INF LLONG_MAX
#define ios ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
using namespace std;
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;}
const int N=3e5+10;
struct node{
int x,y,v;
}a[N];
int fa[N],n,m,cnt=1,sum,idx;
bool cmp(node x,node y){return x.v<y.v;}
int find(int x){
if(x==fa[x])return x;
return fa[x]=find(fa[x]);
}
void he(int x,int y){
int fx=find(x),fy=find(y);
if(fx!=fy)fa[fx]=fy;
}
void k(){
for(int i=0;i<=n;i++)fa[i]=i;
sort(a+1,a+idx+1,cmp);
int i=1;
while(i<=idx&&cnt<=m){
if(find(a[i].x)!=find(a[i].y)){
cnt++;
sum+=a[i].v;
he(a[i].x,a[i].y);
}
i++;
}
}
signed main(){
ios
cin>>n>>m;
for(int i=1;i<=m;i++)a[++idx]={0,i,n};
for(int i=1;i<=m;i++){
for(int j=1;j<=m;j++){
int c;
cin>>c;
if(i<j&&c!=0)a[++idx]={i,j,c};
}
}
k();
cout<<sum;
return 0;
}