Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

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

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

C++: #include<iostream>
using namespace std;
static const int MAX=100;
static const int INFTY=(1<<21);
static const int WHITE=0;
static const int GRAY =1;
static const int BLACK=2;

int n,M[MAX][MAX];

int prim(){
  int u,minv;
  int d[MAX],p[MAX],color[MAX];

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

  while(1){
    minv=INFTY;
    u=-1;
    for(int 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(int v=0;v<n;v++){
      if(color[v]!=BLACK&&M[u][v]!=INFTY){
	if(d[v]>M[u][v]){
	  d[v]=M[u][v];
	  p[v]=u;
	  color[v]=GRAY;
	}
      }
    }
  }
      int sum=0;
      for(int i=0;i<n;i++){
	if(p[i]!=-1) sum+=M[i][p[i]];
      }
      return sum; 
}

int main(){
  cin >> n;

  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int e; cin >> e;
      M[i][j]=(e==-1) ? INFTY:e;
    }
  }

  cout<<prim()<<endl;

  return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

class UF {
private:
    int sz; vector<int> par,nrank;
public:
    UF(){}
    UF(int node_size){ sz = node_size; par.resize(sz),nrank.resize(sz); rep(i,sz){ par[i] = i; nrank[i] = 0; } }
    int find(int x){ if(par[x] == x){ return x; }else{ return par[x] = find(par[x]); } }
    void unite(int x,int y)
    { x = find(x),y = find(y); if(x == y) return;
    	if(nrank[x] < nrank[y]){ par[x] = y; }else{ par[y] = x; if(nrank[x] == nrank[y]) nrank[x]++; } }
    bool same(int x,int y){ return find(x) == find(y); }
};

template<typename T> class Kruskal{
public:
	struct edge{
		int u,v;
		T cost;
		bool operator<(const edge& another) const {
			return cost < another.cost;
		}
	};
	vector<edge> es;
	vector<int> par,rrank;
	int V;
    Kruskal(int node_size) : V{node_size}, par{node_size}, rrank{node_size}{
		// V = node_size;
		// par.resize(node_size),rrank.resize(node_size);
	}
	void add_edge(int u,int v,T cost){
		es.push_back((edge){u,v,cost});
	}
	T make(){
		UF uf(V);
		T res = 0;
		int cnt = 0;
		rep(i,es.size()){
			edge e = es[i];
			if(!uf.same(e.u,e.v)){
				uf.unite(e.u,e.v);
				res += e.cost;
				cnt++;
				if(cnt == V-1){
					break;
				}
			}
		}
		return res;
	}
};


int main()
{
    int n;
    cin >> n;
    Kruskal<int> kr(n);
    rep(i,n){
        rep(j,n){
            int a;
            cin >> a;
            if(i < j){
                kr.add_edge(i,j,a);
            }
        }
    }
    cout << kr.make() << "\n";
}

C++: #include<iostream>
#include<vector>

#define MAX_NODE 100
#define INF 1000000000

using namespace std;

struct Edge{
	int to, w;
};

vector <Edge> e[MAX_NODE];

int minw[MAX_NODE];
bool flag[MAX_NODE];

int prim(int n,int start){
	fill(minw,minw+n,INF);
	fill(flag,flag+n,false);
	minw[start]=0;
	int sum=0;
	int from = start;
	while(1){
		for(int i=0;i<e[from].size();i++){
			if(e[from][i].w<minw[e[from][i].to]){
				minw[e[from][i].to]=e[from][i].w;
			}		
		}
		int minimum_e;
		minimum_e=INF;
		for(int i=0;i<n;i++){
			if(minimum_e>minw[i]&&flag[i]==false){
				minimum_e=minw[i];
				from=i;
			}
		}
		if(minimum_e==INF){
			break;
		}
		sum+=minimum_e;
		flag[from]=true;
	}
	return sum;
}

int main(){
	int n,tmp,sum;
	Edge edge;
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>tmp;
			if(tmp!=-1){
				edge.to=j;
				edge.w=tmp;
				e[i].push_back(edge);
			}
		}
	}
	sum=prim(n,0);
	cout<<sum<<endl;
	return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;

const int INF = 1 << 30;

struct Vertex {
	int id;
	int w;
	vector<Vertex> vtxs;
};

int prim(int n, Vertex vtxs[]){
	
	int d[n];
	bool vst[n];
	for(int i=0;i<n;i++){
		d[i] = INF;
		vst[i] = false;
	}
	
	d[0]=0;
	while(true) {
		int minv = INF;
		int u = -1;
		for(int i=0;i<n;i++){
			if(vst[i]) continue;
			if(minv > d[i]){
				minv = d[i];
				u = i;
			}
		}
		if(u==-1) break;
		vst[u]=true;
		vector<Vertex> vec=vtxs[u].vtxs;
		for(int v=0;v<vec.size();v++){
			Vertex vtx=vec[v];
			int id=vtx.id;
			if(vst[id]) continue;
			d[id]=min(d[id],vtx.w);
		}
	}
	int cost=0;
	for(int i=0;i<n;i++){
		cost+=d[i];
	}
	return cost;
}

int main(){
	int n;
	cin >> n;
	Vertex vtxs[n];
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int w;
			cin >> w;
			if(w==-1) continue;
			Vertex v;
			v.id = j;
			v.w = w;
			vtxs[i].vtxs.push_back(v);
		}
	}
	int cost = prim(n,vtxs);
	cout << cost << endl;
	return 0;
}