Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

const INF = 1 << 60

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		G[i] = make([]int, N)
		for j := 0; j < N; j++ {
			G[i][j] = sc.nextInt()
		}
	}
	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	seen := make([]bool, N)
	d[0] = 0
	ans := 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			if !seen[i] && d[i] < minCost {
				minCost = d[i]
				cur = i
			}
		}
		if minCost == INF {
			break
		}
		seen[cur] = true
		ans += d[cur]
		for i := 0; i < N; i++ {
			cost := G[cur][i]
			if cost != -1 && cost < d[i] {
				d[i] = cost
			}
		}
	}

	fmt.Fprintln(wtr, ans)
	wtr.Flush()
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 105
int matrix[N][N];
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A
//spanning tree tree
bool power[N]; // start from 1;
int main(){
	int n;
	scanf("%d",&n);
	memset(matrix, 0,sizeof(matrix));
	memset(power,0,sizeof(power));
	for(int i = 1; i<=n; i++){
		for(int j = 1; j<=n; j++){
			scanf("%d",&matrix[i][j]);//-1 means nothing
		}
	}
	int count = 0;
	int res = 0;
	int min = 2005;
	while(count<n-1){
		//find minmila ele from matrix
		int v1 = 0,v2 = 0;
		min = 2005;
		for(int i = 1; i<=n;i++){
			for(int j = 1;j<=n; j++){
				if(matrix[i][j]!=-1 && matrix[i][j]<min){
					if(count==0){
						if(power[i] == 0 && power[j]==0) {
							v1 = i;v2 = j;
							//printf("i:%d j:%d\n",i,j);
							min = matrix[i][j];
						}
						//continue;
					}else {
							if((power[i] == 1 && power[j]==0)||(power[i] == 0 && power[j]==1)){
							v1 = i;v2 = j;
							min = matrix[i][j];
						}
					}
					
				}
			}
		}
		//printf("min:%d\n",min);
		matrix[v1][v2] = -1;
		power[v1] = 1;
		power[v2] = 1;
		res+=min;
		count++;
	}
	printf("%d\n",res);
	return 0;
}

C++: /*#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
int main(){
	int n, a, sum = 0;
	int temp = 1000000;
	int n1, n2;
	int r1[101] = {0}, r2[101] = { 0 };
	int m[101][101] = { 0 };
	cin >> n;
	for (int i = 0; i < n; i++){
		for (int l = 0; l < n; l++){
			cin >> a;
			m[i][l] = a;
		}
	}
	for (int i = 0; i < n-1; i++){
		for (int l = 0; l < n; l++){
			cout << m[i][l] << "   " << i <<endl;

			if (m[i][l] == -1) continue;
			if (m[i][l] < temp&&r1[l] == 0 && r2[i] == 0){
				temp = m[i][l];
				n1 = i; n2 = l;
			}
		}
		if (temp != 1000000){
			sum += temp;
			cout << i + 1 << endl;
			cout << temp <<endl;
			r1[n1] = -1; r2[n2] = -1;
			cout << n1  << n2 << endl;
			n1 = 0; n2 = 0;
			temp = 1000000;
		}
	}
	cout << sum << endl;
	return 0;
}*/
/*
5
-1 2 3 1 -1
2 -1 -1 4 -1
3 -1 -1 1 1
1 4 1 -1 3
-1 -1 1 3 -1
*/
#include<queue>
#include<stack>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
#define WHITE 0
#define BLACK 1
#define GRAY 2
#define INFTY 1000000
int color[101];
int m[101][101];
int d[101];
int p[101];
int mincost = INFTY;
int n;
int yy;


void prim(int u){
	while (1){
		mincost = INFTY;
		for (int i = 0; i < n - 1; i++){
			if (color[i] != BLACK&&d[i] < mincost){
				mincost = d[i];
				u = i;
			}
		}
		if (mincost == INFTY) break;
		color[u] = BLACK;
		for (int v = 0; v < n - 1; v++){
			if (color[v] != BLACK&&m[u][v] != INFTY){
				if (m[u][v] < d[v]){
					d[v] = m[u][v];
					p[v] = u;
					color[v] = GRAY;
				}
			}
		}
	}
}
int main(){
	int a[10] = { 0 };
	for (int i = 0; i < 101; i++){
		color[i] = INFTY; d[i] = INFTY; p[i] = -1;
	}
	for (int i = 0; i < 101; i++){
		for (int l = 0; l < 101; l++){
			m[i][l] = 0;
		}
	}
	cin >> n;
	for (int i = 0; i < n; i++){
		for (int l = 0; l < n; l++){
			cin >> m[i][l];
		}
	}
	prim(0);
	int sum = 0;
	for (int i = 0; i < n; i++){
		sum += p[i];
	}
	cout << sum*(-1) << endl;
	return 0;
}

C++: #include<stdio.h>

#define INFTY (1<<21)
#define MAX 100
#define WHITE 0
#define GRAY 1
#define 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(){
  
  scanf("%d",&n);

  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      int e;
      scanf("%d",&e);
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  printf("%d\n",prim());

  return 0;
}

C++: #include <tuple>
#include <iostream>
#include <algorithm>
using namespace std;
int P[10010]; 
tuple<int,int,int> a[10010];
void init(int N) { 
    for (int i=0; i<N; ++i) P[i] = i;
}
int root(int a) { 
    if (P[a] == a) return a; 
    return (P[a] = root(P[a])); 
}
bool is_same_set(int a, int b) {
    return root(a) == root(b);
}
void unite(int a, int b) {
    P[root(a)] = root(b);
}
int main(void){
    int n,value;
    cin >> n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin >> value;
            a[n*i+j] = make_tuple(value,i,j);
        }
    }
    init(n*n);
    sort(a,a+n*n);
    int sum =0;
    int x,y;
    for(int i =0;i<n*n;i++){
        x = get<1>(a[i]);
        y = get<2>(a[i]);
        if(is_same_set(x,y)!=1){
            if(get<0>(a[i])!=-1){
                sum += get<0>(a[i]);
                unite(x,y);
            }
        }
    }
    cout << sum << endl;
}