#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct EDGE
{ int beg; int end; int cost; int operator <(const struct EDGE& tar) const { return cost < tar.cost; }};
int parent[150]={0};
int Find(int f){ while ( parent[f] > 0) { f = parent[f]; } return f;}
int unionl(EDGE edge)
{ int n = Find(edge.beg); int m = Find(edge.end);
if (n != m)
{ parent[n] = m; return 1; }
return 0;
}
long long ans = 0;
int main()
{
vector<EDGE> edges; EDGE tedge;
int N; cin>>N;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
int tco;
cin>>tco;
if(tco!=0)
{ tedge.beg=i; tedge.end=j; tedge.cost=tco; edges.push_back(tedge); }
}
sort(edges.begin(),edges.end());
for(int i=0;i<edges.size();i++)
{ if(unionl(edges[i])) { ans += edges[i].cost; } }
cout << ans ;
return 0;
}