Go: package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <functional>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;
typedef pair<int, int> P;

double distanceAB(double xa, double ya, double xb, double yb);
//void trace(int A[], int N);

/*
class Target
{
public:
	vector <string> draw(int n)
	{

	}
};
*/


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


int main()
{
	cin >> n;
	
	rep(i, n)
	{
		rep(j, n)
		{
			int e;
			cin >> e;
			M[i][j] = (e == -1) ? INFTY : e;
		}
	}
	
	cout << prim() << endl;
	
	return 0;
}


/*
void trace(int A[], int N)
{
	REP(N)
	{
		if (i > 0) cout << " ";
		cout << A[i];
	}
	cout << endl;
}
*/


double distanceAB(double xa, double ya, double xb, double yb)
{
	return sqrt((xb-xa)*(xb-xa) + (yb-ya)*(yb-ya));
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

template <class T>
using lp_queue=priority_queue<T, vector<T>, greater<T>>;

using Weight=int;
using DArray=vector<Weight>;
using DMatrix=vector<DArray>;

static const Weight INF=1<<29;

struct Edge {
    size_t src, dst;
    Weight weight;
    Edge() {}
    Edge(size_t src, size_t dst, Weight weight=1):
        src(src), dst(dst), weight(weight)
    {}
};

bool operator<(const Edge &e, const Edge &f) {
    if (e.weight != f.weight) {
        return e.weight > f.weight;
    } else {
        return e.src!=f.src? e.src<f.src : e.dst<f.dst;
    }
}

using Edges=vector<Edge>;
using Vertex=vector<Edge>;
using Graph=vector<Vertex>;

void join(Graph &g, size_t s, size_t d, Weight w=1) {
    // non-directed
    g[s].push_back(Edge(s, d, w));
    g[d].push_back(Edge(d, s, w));
}

void connect(Graph &g, size_t s, size_t d, Weight w=1) {
    // directed
    g[s].push_back(Edge(s, d, w));
}

Weight minimum_spanning_tree(const Graph &g, size_t s) {
    /* Prim */
    size_t V=g.size();
    vector<Weight> d(V, INF); d[s]=0;
    Weight res=0;

    lp_queue<pair<Weight, size_t>> q; q.push(make_pair(0, s));
    while (!q.empty()) {
        pair<Weight, size_t> p=q.top(); q.pop();
        size_t v=p.second;
        if (d[v] < p.first) continue;

        res += d[v];
        for (const Edge &e: g[v])
            if (d[e.dst] > e.weight) {
                d[e.dst] = e.weight;
                q.push(make_pair(d[e.dst], e.dst));
            }
    }
    return res;
}

int main() {
    size_t n;
    scanf("%zu", &n);

    Graph g(n);
    for (size_t i=0; i<n; ++i)
        for (size_t j=0; j<n; ++j) {
            Weight a;
            scanf("%d", &a);

            if (a > -1)
                connect(g, i, j, a);
        }

    printf("%d\n", minimum_spanning_tree(g, 0));
    return 0;
}

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

using namespace std;

int G[110][110];
int cost[1000],src[1000],dst[1000];

int P[10010];
int root(int a);
int is_same_set(int a,int b);
void unite(int a,int b);

int main() {
	int N,a;
	cin >> N;
	for(int i=0; i<N;i++) {
		for(int j=0;j<N;j++) {
			cin >> a;
			G[i][j] = a;
		}
	}
	int hcon = 1;
	int M = 0;
	pair<int,int> edges[10000];

	for(int r=0;r<N;r++) {
		for (int c=0;c<hcon;c++) {
			if(G[r][c] > -1) {
				cost[M] = G[r][c];
				src[M] = r;
				dst[M] = c;
				edges[M] = make_pair(cost[M],M);
				M+=1;
			}
		}
		hcon++;
	}
	sort(edges,edges+M);



	for(int i=0;i<N;i++) P[i] = i;

	int answer = 0;
	for(int i=0;i<M;i++) {
		if(!is_same_set(src[edges[i].second],dst[edges[i].second])) {
			unite(src[edges[i].second],dst[edges[i].second]);
			answer += edges[i].first;
			//cout << answer << endl;
		}
	}
	cout << answer << endl;;
}

int root(int a) { 
	if (P[a] == a) return a; 
	return (P[a] = root(P[a])); 
}
int is_same_set(int a, int b) { 
	if(root(a) == root(b)) {
		return 1;
	} else {
		return 0;
	}
}
void unite(int a, int b) { 
	P[root(a)] = root(b);
}

C++: #include <iostream>
#include <climits>
#include <queue>

using namespace std;
static const int MAX=100;
static const int WHITE=1;
static const int GRAY=2;
static const int BLACK=3;

int n,M[MAX+10][MAX+10];

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

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

    d[0]=0;

    while(1)
    {
        minv=INT_MAX;
        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]!=INT_MAX)
        {
            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)? INT_MAX:e;
    }

    cout<<prim()<<endl;
    return 0;
}