Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include <iostream>

using namespace std;

int main() {
    int n, matrix[100][100];

    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> matrix[i][j];
        }
    }

    bool visit[100] = {
        true
    };
    int sum = 0;

    while (1) {
        int min = 2001, mini = -1;
        for (int i = 0; i < n; i++) {
            if (visit[i] == true) {
                for (int j = 0; j < n; j++) {
                    if (matrix[i][j] > -1 && matrix[i][j] < min && visit[j] == false) {
                        min = matrix[i][j];
                        mini = j;
                    }
                }
            }
        }
        if (mini == -1) {
            break;
        }
        else {
            sum += min;
            visit[mini] = true;
        }
    }

    cout << sum << endl;

    return 0;
}

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

struct edge {int u, v, cost;};

int n;
vector<edge> G;

bool comp(const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}

class UnionFind {
 public:
    int g_num;
    vector<int> rank, par;

    UnionFind() {}
    UnionFind(int size) {
        rank.resize(size, 0);
        par.resize(size, 0);
        for (int i = 0; i < size; i++) {
            par[i] = i;
            rank[i] = 0;
        }
        g_num = size;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;

        g_num--;
        if (rank[x] > rank[y]) {
            par[y] = x;
        } else {
            par[x] = y;
            if (rank[x] == rank[y]) rank[y]++;
        }
    }

    int find(int x) {
        if (x == par[x]) return x;
        return par[x] = find(par[x]);
    }

    int group_num() {
        return g_num;
    }
};

int prim() {
    sort(G.begin(), G.end(), comp);
    UnionFind uf(n);
    int res = 0;
    for (auto e: G) {
        if (!uf.same(e.u, e.v)) {
            uf.unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int cost;
            cin >> cost;
            if (cost == -1) continue;
            G.push_back(edge {i, j, cost});
        }
    }
    int ans = prim();
    cout << ans << endl;
    return 0;
}

C++: //============================================================================
// Name        : MinimumSpanningTree.cpp
// Author      : Ken
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
#include <set>
using namespace std;

struct Edge;

struct Node{
	const int id;
	vector<int> adjEdgeIds;
};

struct Edge{
	int id;
	int weight;
	int startNodeId;
	int endNodeId;
};

class Graph{
public:
	int MAX_WEIGHT=2000;
	vector<Node> nodeList;
	vector<Edge> edgeList;

	Graph():nodeList(),edgeList(){}

	Graph(int n):nodeList(){
		for(int i=0; i<n; i++){
			vector<int> empty;
			Node node = {i, empty};
			nodeList.push_back(node);
		}
		//cout << "initialized\n";
	}

	void write(){
		cout << "***nodeList***\n";
		for(int i=0; i<nodeList.size(); i++){
			Node& node = nodeList.at(i);
			cout << "node id: " << node.id << " ";
			for(int j=0; j<node.adjEdgeIds.size(); j++){
				Edge& edge = edgeList.at(node.adjEdgeIds.at(j));
				cout << "edge id: " << edge.id << " ";
				cout << edge.startNodeId << "<->" << edge.endNodeId << " ";
			}
			cout << "\n";
		}

		cout << "***edgeList***\n";
		for(int i=0; i<edgeList.size(); i++){
			Edge& edge = edgeList.at(i);
			cout << "edge id: " << edge.id << " ";
			cout << "edge weight: " << edge.weight << " ";
			cout << edge.startNodeId << "<->" << edge.endNodeId << "\n";
		}
	}

	void addLightestEdge(set<int>& edgeIds){
		set<int> curNodeIds;
		if(edgeIds.size()==0){
			curNodeIds.insert(0);
		}else{
			set<int>::iterator p;
			for(p=edgeIds.begin(); p!=edgeIds.end(); p++){
				Edge& edge = edgeList.at(*p);
				curNodeIds.insert(edge.startNodeId);
				curNodeIds.insert(edge.endNodeId);
			}
		}

		while(true){
		set<int>::iterator p;
		int lightestWeight = MAX_WEIGHT;
		int lightestEdgeId = -1;
		for(p=curNodeIds.begin(); p!=curNodeIds.end(); p++){
			Node& node = nodeList.at(*p);

			vector<int>::iterator q;
			for(q=node.adjEdgeIds.begin(); q!=node.adjEdgeIds.end(); q++){

				if(curNodeIds.count(edgeList.at(*q).startNodeId) == 0 or
				   curNodeIds.count(edgeList.at(*q).endNodeId) == 0){

					if(edgeList.at(*q).weight < lightestWeight){
						lightestWeight = edgeList.at(*q).weight;
						lightestEdgeId = edgeList.at(*q).id;
					}
				}
			}
		}

		if(lightestEdgeId!=-1){
		    edgeIds.insert(lightestEdgeId);
		    //cout << "inserted edge id: " << lightestEdgeId << "\n";
		    //cout << "inserted edge weight: " << lightestWeight << "\n";
		    Edge& edge = edgeList.at(lightestEdgeId);
		    curNodeIds.insert(edge.startNodeId);
		    curNodeIds.insert(edge.endNodeId);
		}else{
			return;
		}
		}
	}

	int sumEdgeWeight(set<int> edgeIdSet){
		int sum = 0;
		set<int>::iterator p;
		for(p=edgeIdSet.begin(); p!=edgeIdSet.end(); p++){
			sum += edgeList.at(*p).weight;
		}

		return sum;
	}
};

void writeSet(set<int> intSet){
	set<int>::iterator p;
	for(p=intSet.begin(); p!=intSet.end(); p++){
		cout << *p << " ";
	}
}



int main() {
	int n;
	cin >> n;
	Graph g(n);

	//g.write();

	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			int weight;
			cin >> weight;
			if(i<j and weight != -1){
				int edgeId = g.edgeList.size();
				Edge edge = {edgeId, weight, i, j};
				g.edgeList.push_back(edge);
				g.nodeList.at(i).adjEdgeIds.push_back(edgeId);
				g.nodeList.at(j).adjEdgeIds.push_back(edgeId);
			}
		}
	}

	//g.write();

	set<int> edgeIds;
	g.addLightestEdge(edgeIds);
	//writeSet(edgeIds);
	int totalWeight;
	totalWeight = g.sumEdgeWeight(edgeIds);
	cout << totalWeight << "\n";

	return 0;
}