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 <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 101;
constexpr int INF = 1<<21;
enum Color { WHITE = 0, GRAY, BLACK };
int n;
vv(int, M, MAX, MAX, INF);
inline int prim() {
	vi d(MAX, INF), t(MAX, -1), color(MAX, WHITE);
	d[0] = 0;
	int minv, u;
	while(true) {
		minv = INF;
		u = -1;
		rep(i, 0, n) {
			if(minv > d[i] && color[i] != BLACK) {
				u = i;
				minv = d[i];
			}
		}
		if(u == -1) break;
		color[u] = BLACK;
		rep(v, 0, n) {
			if(color[v] != BLACK && M[u][v] != INF && d[v] > M[u][v]) {
				d[v] = M[u][v];
				t[v] = u;
				color[v] = GRAY;
			}
		}
	}
	int sum = 0;
	rep(i, 0, n) {
		if(t[i] != -1) sum += M[i][t[i]];
	}
	return sum;
}
int main() {
	cin >> n;
	int e;
	rep(i, 0, n) {
		rep(j, 0, n) {
			cin >> e;
			if(e != -1) M[i][j] = e;
		}
	}
	P(prim());
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,M[105][105];
int prim(){
  int u,minv;
  int d[105],p[105],color[105];
  for(int i=0;i<n;i++){
    d[i]=1234567890;
    p[i]=-1;
    color[i] = 0;
  }
  d[0]=0;
  while(1){
    minv = 1234567890;
    u = -1;
    for(int i=0;i<n;i++){
      if(minv > d[i] && color[i]!=2){
	u=i;
	minv = d[i];
      }
    }
    if(u==-1)break;
    color[u]=2;
    for(int v=0;v<n;v++){
      if(color[v]!=2&&M[u][v]!=1234567890){
	if(d[v]>M[u][v]){
	  d[v]=M[u][v];
	  p[v]=u;
	  color[v]=1;
	}
      }
    }
  }
  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)? 1234567890 : e;
    }
  }
  cout << prim() << endl;
  return 0;
}

C++: #include <stdio.h>

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
const int N = 100;
const int INF = 2000000000;

int g[N + 1][N + 1];
int d[N + 1], p[N + 1], color[N + 1];
int n;

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

	while (true)
	{
		int min = INF;
		int u = -1;
		for (int i = 1; i <= n; i++)
		{
			if (color[i] != BLACK && d[i] < min)
			{
				min = d[i];
				u = i;
			}
		}
		if (u == -1)
			break;

		color[u] = BLACK;

		for (int i = 1; i <= n; i++)
		{
			if (color[i] != BLACK && g[u][i] != INF && d[i] > g[u][i])
			{
				d[i] = g[u][i];
				color[i] = GRAY;
				p[i] = u;
			}
		}
	}
	int sum = 0;
	for (int i = 1; i <= n; i++)
	{
		if (p[i] != -1)
			sum += g[p[i]][i];
	}
	printf("%d\n", sum);
}

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			scanf("%d", &g[i][j]);
			if (g[i][j] == -1)
				g[i][j] = INF;
		}
	prim();

	return 0;
}

C++: #include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 105;

int m[105][105];
int n,e;

int prim(int s)
{
    int minv,cnt[MAXN],k,ans = 0;
    for(int i = 1; i <= n; ++i)
        cnt[i] = m[s][i];
    cnt[s] = 0;
    for(int i = 1; i < n; ++i)
    {
        minv = INF;
        for(int j = 1; j <= n; ++j)
        {
            if(cnt[j] && cnt[j]<minv)
                minv = cnt[j],k = j;
        }
        cnt[k] = 0;
        ans += minv;
        for(int j = 1; j <= n; ++j)
            if(m[k][j] < cnt[j])
                cnt[j] = m[k][j];
    }
    return ans;
}

int main(void)
{
    cin>>n;
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            cin>>e;
            m[i][j] = e==-1?INF:e;
        }
    }
    cout<<prim(1)<<'\n';
    return 0;
}