// prim
intT,M,N,K,I,a,b,c,ans,cnt;intdis[105][105];boolmst[105];priority_queue<pii>q;voidinit(){memset(mst,0,sizeof(mst));ans=0;for(inti=0;i<N;i++)for(intj=0;j<N;j++)cin>>dis[i][j];}voidsol(){intv,w;mst[0]=1;for(inti=0;i<N;i++)if(dis[0][i])q.push(MP(-dis[0][i],i));while(!q.empty()){v=q.top().Y,w=-q.top().X;q.pop();if(mst[v])continue;mst[v]=1;ans+=w;for(inti=0;i<N;i++)if(!mst[i]&&dis[v][i])q.push(MP(-dis[v][i],i));}cout<<ans<<endl;}intmain(){ios::sync_with_stdio(false);cin.tie(0);while(cin>>N){init();sol();}}