#include<bits/stdc++.h>
using namespace std;
#define I(e) for(int i=0;i<e;i++)
#define J(e) for(int j=0;j<e;j++)
#define K(w,e) for(int k=w;k<e;k++)
int fa[1005];
struct P{
int fm,to,w;
}p[1005];
bool cmp(P a,P b){
return a.w<b.w;
}
int got(int x){
if(fa[x]==x){
return x;
}
else{
return got(fa[x]);
}
}
void morge(int a,int b){
fa[got(a)]=got(b);
}
int main(){
int a,b;
int y;
scanf("%d%d",&a,&b);
int ct=0;
I(b){
J(b){
fa[ct]=ct;
int h;
scanf("%d",&h);
p[ct].w=h;
if(h==0){
p[ct].w=a;
}
p[ct].fm=i;
p[ct].to=j;
ct++;
}
}
sort(p,p+(b*b),cmp);
int ans=a;
I(b*b){
int x=got(p[i].fm),y=got(p[i].to);
if(x!=y){
morge(x,y);
ans+=p[i].w;
}
i++;
}
cout<<ans;
return 0;
}