#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
int s, e, w;
node(int ss, int ee, int ww):s(ss),e(ee),w(ww){}
bool operator < (const node & n) const {
return w<n.w;
}
};
vector<node>edges;
vector<int>parent;
int GetRot(int a)
{
if(a!=parent[a])
parent[a]=GetRot(parent[a]);
return parent[a];
}
void Union(int a, int b)
{
a = GetRot(a);
b = GetRot(b);
if(a==b)
return ;
parent[a]=b;
}
int main()
{
int N;
while(cin>>N)
{
edges.clear(); parent.clear();
for(int i=0; i<N; i++)
parent.push_back(i);
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
{
int w; cin>>w;
edges.push_back(node(i,j,w));
}
sort(edges.begin(), edges.end());
int done = 0; int ans=0;
for(int i=0; i<edges.size(); i++){
if( GetRot(edges[i].s)!=GetRot(edges[i].e) )
{
Union(edges[i].s,edges[i].e);
ans+=edges[i].w;
done++;
}
if(done==N-1) break;
}
cout<<ans<<endl;
}
return 0;
}