#include<bits/stdc++.h>
using namespace std;
int a,b,st[510][510],dist[510],ans;
bool buy[510];
void prim(){
for(int i=1;i<=b;i++)
{
int t=-1;
for(int j=1;j<=b;j++)
{
if(!buy[j]&&(t==-1||dist[t]>dist[j]))
{
t=j;
}
}
if(i!=1) ans+=dist[t];
buy[t]=1;
for(int j=1;j<=b;j++) if(!buy[j]) dist[j]=min(dist[j],st[t][j]);
}
}
int main()
{
memset(dist,0x7f,sizeof(dist));
cin>>a>>b;
ans=a;
for(int i=1;i<=b;i++)
{
for(int j=1;j<=b;j++)
{
cin>>st[i][j];
if(st[i][j]==0)
{
st[i][j]=a;
}
}
}
prim();
cout<<ans;
}