Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for 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
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

C++: #include <cstdio>
using namespace std;
#define MAXN 100
#define INFTY (1 << 21)
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[MAXN][MAXN];

int prim()
{
	int d[MAXN], p[MAXN], color[MAXN];
	int u, minv;
	for(int i = 0; i < n; i++)
	{
		d[i] = INFTY;
		p[i] = -1;
		color[i] = WHITE;
		//printf("===\n");
	}
	d[0] = 0;
	while(true)
	{
		u = -1;
		minv = INFTY;
		for(int i = 0; i < n; i++)
		{
			if(color[i] != BLACK && d[i] < minv)
			{
				u = i;
				minv = d[i];
				//printf("%d \n", u);
			}
		}
		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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1LL << 60;
const int MAX = 1000000010;

vector<bool> checked;
vector<int> d;
vector<int> p;

int main() {
    int N; cin >> N;
    vector<vector<int>> G(N, vector<int>(N));
    checked.assign(N, false);
    d.assign(N, MAX);
    p.resize(N);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cin >> G[i][j];
        }
    }
    d[0] = 0;
    p[0] = -1;

    while (true) {
        int MIN = MAX;
        int u;
        for (int i = 0; i < N; i++) {
            if(!checked[i] && chmin(MIN, d[i])) u = i;
        }

        if (MIN == MAX) break;
        checked[u] = true;

        for (int v = 0; v < N; v++) {
            if (!checked[v] && G[u][v] != -1) {
                chmin(d[v], G[u][v]);
                p[v] = u;
            }
        }
    }

    int sum = 0;
    for (int i = 0; i < N; i++) {
        sum += d[i];
    }

    cout << sum << endl;
    return 0;
}

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

const int MAX = 100;
const int INFINITY = 2 << 21;
int n;
int G[MAX][MAX];
int distances[MAX];
int parents[MAX];
int states[MAX];

int prim() {
  int id = 0;
  distances[id] = 0;

  while (1) {
    int min = INFINITY;

    for (int i = 0; i < n; i++) {
      if (states[i] == 0 && distances[i] < min) {
        min = distances[i];
        id = i;
      }
    }

    if (min == INFINITY) break;

    for (int i = 0; i < n; i++) {
      if (states[i] != 0 || G[id][i] == -1) continue;
      if (G[id][i] < distances[i]) {
        distances[i] = G[id][i];
        parents[i] = id;
      }
    }

    states[id] = 1;
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (parents[i] != -1) sum += distances[i];
  }
  return sum;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;

  for (int i = 0; i < n; i++) {
    distances[i] = INFINITY;
    parents[i] = -1;
    states[i] = 0;
    for (int j = 0; j < n; j++) {
      cin >> G[i][j];
    }
  }

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

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

int n;
int a[101][101];
int flg[101];
int t[101];
int sum = 0;

void visit(){
  int ima = 0;
  int mine = 100000000;
  
  
  for(int k=0;k<n-1;k++) {
    mine = 10000000;
    for(int i=0;i<n;i++) {
      if(flg[i] == 0 && mine > t[i]) {
	mine = t[i];
	ima = i;
      }
    }
    sum += mine;
    flg[ima] = 1;
    
    for(int i=0;i<n;i++) 
      if(a[ima][i] != -1) 
	if(t[i] > a[ima][i] ) t[i] = a[ima][i];
    
    
  }
}

int main() {

  cin >> n;

  for(int i=0;i<n;i++) 
    for(int j=0;j<n;j++) 
      cin >> a[i][j];
  
  for(int i=0;i<n;i++) {
    flg[i] = 0;
    t[i] = 100000000;
  }
  
  t[0] = 0;
  flg[0] = 1;
  for(int i=0;i<n;i++) 
    if(a[0][i] != -1) t[i] = a[0][i];

  visit();

  cout << sum << endl;
 
  return 0; 
  
}

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

int n;
int g[101][101];

int d[101];
char c[101];
int p[101];

void prim() {
    for (int i = 0; i < n; ++i) {
        d[i] = INT_MAX;
        c[i] = 'w';
    }
    c[0] = 'b';
    d[0] = 0;
    while (true) {
        int u, v;
        int mine = INT_MAX;
        for (int j = 0; j < n; ++j) {
            if (c[j] == 'b') {
                u = j;
                for (int k = 0; k < n; ++k) {
                    if(g[u][k] > -1 && g[u][k] <= d[k] && c[k] != 'b' && g[u][k] <= mine) {
                        d[k] = g[u][k];
                        mine = d[k];
                        v = k;
                    }
                }
            }
        }
        p[v] = u;
        c[v] = 'b';
        if(mine == INT_MAX) break;
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int w;
            cin >> w;
            g[i][j] = w;
        }
    }
    prim();
    int sum;
    for (int k = 0; k < n; ++k) {
        sum += d[k];
    }
    cout << sum << endl;


    return 0;
}