#include<bits/stdc++.h>
using namespace std;
struct edge
{
int to;
int weight;
};
vector<edge> head[1001];
bool operator<(const edge &a,const edge &b)
{
return a.weight>b.weight;
}
priority_queue<edge> q;
bool vis[1000];
int main()
{
int i,j,k,u,v,n,ans=0;
edge e;
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>k;
if(k>0)
{
head[i].push_back({j,k});
}
}
}
vis[1]=1;
for(i=0;i<head[1].size();i++)
{
q.push(head[1][i]);
}
for(i=1;i<n;i++)
{
while(!q.empty())
{
e=q.top();
q.pop();
if(!vis[e.to])
{
ans+=e.weight;
vis[e.to]=1;
break;
}
}
}
u=e.to;
for(j=0;j<head[u].size();j++)
{
if(!vis[head[u][j].to])
{
q.push(head[u][j]);
}
}
cout<<ans;
return 0;
}