Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	MAX   = 100
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [MAX][MAX]int

func (g *Graph) prim(n int) int {
	var (
		u, minv     = 0, 0
		d, p, color [MAX]int
	)

	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}
	d[0] = 0

	for {
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for v := 0; v < n; v++ {
			if color[v] != BLACK && g[u][v] != INFTY {
				if d[v] > g[u][v] {
					d[v] = g[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += g[i][p[i]]
		}
	}

	return sum
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			e, _ := strconv.Atoi(sc.Text())
			if e == -1 {
				g[i][j] = INFTY
				continue
			}
			g[i][j] = e
		}
	}

	return fmt.Sprint(g.prim(n))
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
using P = pair<int,int>;

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_N = 105, INF = 10000;
const int WHITE = 0, GRAY = 1, BLACK = 2;

int n;
int M[MAX_N][MAX_N], color[MAX_N], d[MAX_N], p[MAX_N];

void init(){
    rep(i,MAX_N){
        rep(j,MAX_N) M[i][j] = -1;
        color[i] = WHITE;
        d[i] = INF;
    }
}

void prim(int u){
    d[u] = 0;
    p[u] = -1;

    while(true){
        int mincost = INF;
        rep(i,n){
            if(color[i] != BLACK && mincost > d[i]){
                mincost = d[i];
                u = i;
            }
        }
        if(mincost == INF) break;
        color[u] = BLACK;
        rep(v,n){
            if(color[v] != BLACK && M[u][v] != -1){
                if(M[u][v] < d[v]){
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
}

int main()
{
    cin >> n;
    init();
    rep(i,n){
        rep(j,n){
            int aij; cin >> aij;
            if(aij != -1) M[i][j] = aij;
        }
    }
    prim(0);
    int sum = 0;
    rep(i,n) sum += d[i];
    cout << sum << endl;
}

C++: #include "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto& x:c){cout<<x<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

class Edge{
public:
	ll to,weight;
	bool use{true};
	Edge(ll t,ll w):to(t),weight(w){}
};

class Edge2{
public:
	ll from,to,weight;
	Edge2()=default;
	Edge2(ll f,ll t,ll w):from(f),to(t),weight(w){}
};

class ConnectNodeInfo{
	vector<vector<Edge>> graph;
public:
	ConnectNodeInfo(int node_num){
		graph.resize(node_num);
	}
	void AddNonDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
		graph[v].emplace_back(u,w);
	}
	void AddDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
	}
	vector<Edge>& operator[](ll index){
		return graph[index];
	}
	size_t size(){return graph.size();}
};

class SearchPath{
	set<ll> visit;

	bool CanReachImpl(int start,int end,ConnectNodeInfo& connect){
		bool reach=false;
		if(start==end) return true;

		for(int i=0,ei=connect[start].size();i<ei;i++){
			if(!connect[start][i].use) continue;
			auto res = visit.emplace(connect[start][i].to);
			if(!res.second) continue;
			reach |= CanReachImpl(connect[start][i].to,end,connect);
		}
		return reach;
	}
public:
	bool CanReach(int start,int end,ConnectNodeInfo& connect){
		visit.clear();
		visit.emplace(start);
		return CanReachImpl(start,end,connect);
	}
};

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N=in();
	vector<vector<int>> C(N,vector<int>(N));
	ConnectNodeInfo connect(N);
	vector<Edge2> edge;
	REP(i,N){
		REP(j,N){
			C[i][j]=in();
		}
	}
	REP(i,N){
		for(int j=i;j<N;j++){
			int v=C[i][j];
			if(v!=-1){
				connect.AddNonDirectionalConnection(i,j,v);
				edge.emplace_back(i,j,v);
			}
		}
	}
	sort(ALL(edge),[](Edge2& l,Edge2& r){return l.weight>r.weight;});
	SearchPath sp;
	int M=edge.size();
	ll sum=0;
	REP(i,M){
		auto& e=edge[i];
		for(auto& x:connect[e.from]){
			if(x.to==e.to) x.use=false;
		}
		for(auto& x:connect[e.to]){
			if(x.to==e.from) x.use=false;
		}
		if(!sp.CanReach(e.from,e.to,connect)){
			for(auto& x:connect[e.from]){
				if(x.to==e.to) x.use=true;
			}
			for(auto& x:connect[e.to]){
				if(x.to==e.from) x.use=true;
			}
			sum+=e.weight;
		}
	}
	
	out(sum);
	return 0;
}