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 a, min;
   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 ) {
      min = INFTY;
      a = -1;
   
      for ( int i = 0; i < n; i++ ) {
         if ( min > d[i] && color[i] != BLACK ) {
            a = i;
            min = d[i];
         }
      }
   
      if ( a == -1 ) break;
      color[a] = BLACK;
      for ( int v = 0; v < n; v++ ) {
         if ( color[v] != BLACK && M[a][v] != INFTY ) {
            if ( d[v] > M[a][v] ) {
               d[v] = M[a][v];
               p[v] = a;
               color[v] = GRAY;
            }
         }
      }
   }
   
   int sum = 0;
   for ( int i = 0; i < n; i++ ) {
      if ( p[i] != -1 ) sum = 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 <iostream>
#include <vector>
#include <queue>
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[u][v]?????°???u??°v???????????????
int M[MAX][MAX];

struct segment{
    //??????
    int start;
    //??????
    int end;
    //??????
    int dis;

    segment(int s,int e,int d):start(s),end(e),dis(d){}

    bool operator < (const segment& p)const {
        return dis<p.dis;
    }
};

int prim(){
    //????????¶???
    int u=0;
    //??¨?????°???MST?????¶???V?????¶??????
    int p[MAX];
    //??¨?????°???v???????????¶???WHITE???GRAY???BLACK
    int color[MAX];
    //??¨?????°????????\T?????¶??????V-T?????¶?????????????????????????°????????????????
    priority_queue<segment,vector<segment> > d;

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

    while(true){
        bool ok=true;
        for(int i=0;i<n;i++)
            if(color[i]==WHITE){
                ok= false;
                break;
            }
        if(ok)
            break;

        color[u]=BLACK;
        for(int i=0;i<n;i++)
            if(i!=u&&color[i]==WHITE&&M[u][i]!=INFTY){
                color[i]=GRAY;
                d.push(segment(u,i,M[u][i]));
            }
        u=d.top().end;
        p[u]=d.top().start;
        d.pop();
    }

    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;
}

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

#define NIL -1
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 1 << 21

int prim(vector<vector<int> > &graph, int n){

    vector<int> parent(n, NIL);
    vector<int> cost(n, INF);
    vector<int> color(n, WHITE);

    cost[0] = 0;
    while(1){
        int minv = INF;
        int u = NIL;
        rep(i, n){
            if(cost[i] < minv && color[i] != BLACK){
                minv = cost[i];
                u = i;
            }
        }
        if(u == NIL) break;

        color[u] = BLACK;
        rep(j, n){
            if(color[j] != BLACK && graph[u][j] != NIL){
                if(cost[j] > graph[u][j]){
                    cost[j] = graph[u][j];
                    parent[j] = u;
                    color[j] = GRAY;
                }
            }
        }
    }

    int sum = 0;
    rep(i, n){
        if(parent[i] != NIL){
            sum += graph[i][parent[i]];
        }
    }
    return sum;
}

int main() {

    int n;
    cin >> n;
    vector<vector<int> > graph(n, vector<int>(n));
    rep(i, n){
        rep(j, n){
            cin >> graph[i][j];
        }
    }

    cout << prim(graph, n) << endl;

    return 0;
}

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

using namespace std;

template <class T>
using gp_queue=priority_queue<T, vector<T>, less<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));
}

pair<Weight, Edges> minimum_spanning_tree(const Graph &g, size_t s=0) {
    /* Prim */
    size_t V=g.size();
    Edges T;
    Weight total=0;

    vector<bool> visited(V);
    gp_queue<Edge> q; q.push(Edge(-1, s, 0));
    while (!q.empty()) {
        Edge e=q.top(); q.pop();
        if (visited[e.dst]) continue;

        T.push_back(e);
        total += e.weight;
        visited[e.dst] = true;
        for (const Edge &f: g[e.dst])
            if (!visited[f.dst])
                q.push(f);
    }

    return make_pair(total, T);
}

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).first);
    return 0;
}