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>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <utility>
#include <functional>
#include <numeric>

template <class T>
using vec = std::vector<T>;

constexpr int nil = -1;
constexpr int inf = 1 << 30;

struct Edge
{
    int to;
    int cost;
};

int prim(const vec<vec<Edge>>& edges)
{
    const int n = edges.size();
    vec<bool> reached(n);
    vec<int> parents(n, nil);
    vec<int> distances(n, inf);

    distances[0] = 0;

    while (true)
    {
        int minCost {inf};
        int u {};
        
        for (int i = 0; i < n; ++i)
        {
            if (!reached[i] && distances[i] < minCost)
            {
                u = i;
                minCost = distances[i];
            }
        }
        
        if (minCost == inf)
            break;

        reached[u] = true;

        for (auto e : edges[u])
        {
            if (!reached[e.to])
            {
                if (e.cost < distances[e.to])
                {
                    distances[e.to] = e.cost;
                    parents[e.to] = u;
                }
            }
        }
    }

    int sum = std::accumulate(distances.begin(), distances.end(), 0);

    return sum;
}

void solve()
{
    using namespace std;
    int n;
    cin >> n;

    //隣接リスト形式
    vec<vec<Edge>> edges(n);

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0, cost; j < n; ++j)
        {
            cin >> cost;
            if (cost != -1)
                edges[i].push_back({j, cost});
        }
    }

    cout << prim(edges) << endl;
}

int main()
{
    solve();
    return 0;
}

C++: #include<stdio.h>

#define MAX 1000
#define INFINITY 10000

int n;
int M[MAX][MAX], color[MAX], d[MAX], p[MAX];

enum {
    WHITE,
    GRAY,
    BLACK
};

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

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

        color[target] = BLACK;

        for (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && M[target][v] != -1) {
                if(M[target][v] < d[v]) {
                    d[v] = M[target][v];
                    p[v] = target;
                    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) {
            scanf("%d", &M[i][j]);
        }
    }
    printf("%d\n", prim());
    return 0;
}

C++: #include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
static const int Max = 100;
static const int Infinit = 1000000; // ??????
static const int Nil = -1; // 

enum Status {
	Not_Searched = 0,
	Searched = 1,
	Mts = 2 
};

int n;
int M[Max][Max] = {};
Status st[Max] = {}; 
int min_cost[Max] = {}; // i ??????????????????????????§???????°??????????
int parent[Max] = {}; // ??????index

int prim_search() {
	for (int i = 0; i < n; i++) {
		min_cost[i] = Infinit;
		parent[i] = -1;
		st[i] = Not_Searched;
	}
	min_cost[0] = 0;

	while (1) {
		int mincost = Infinit;
		int mincost_idx = -1;

		for (int i = 0; i < n; i++) {
			if (st[i] != Mts && min_cost[i] < mincost) {
				mincost = min_cost[i];
				mincost_idx = i;
			}
		}
		if (mincost_idx == -1) {
			break;
		}
		st[mincost_idx] = Mts;
		for (int i = 0; i < n; i++) {
			if (st[i] != Mts && M[mincost_idx][i] != Infinit) {
				if (min_cost[i] > M[mincost_idx][i]) {
					min_cost[i] = M[mincost_idx][i];
					parent[i] = mincost_idx;
					st[i] = Searched;
				}
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (parent[i] != Nil) sum += M[i][parent[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) ? Infinit : e;
		}
	}

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

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 2100

int main() {
  int n; cin >> n;
  vector<vector<int>> G(n, vector<int>(n));
  rep(i, n)rep(j, n) cin >> G.at(i).at(j);

  vector<int> color(n); vector<int> mindis(n);
  rep(i, n) mindis.at(i) = INF;
  vector<int> par(n);
  int ans = 0;
  mindis.at(0) = 0;
  color.at(0) = GRAY;
  par.at(0) = -1;

  while (true) {
    int m = INF;
    int now = 0;
    rep(i, n) {
      if (color.at(i) == GRAY && mindis.at(i) < m) {
        m = mindis.at(i);
        now = i;
      }
    }

    if (m == INF) break;

    color.at(now) = BLACK;
    if (par.at(now) != -1) {
      ans += G.at(par.at(now)).at(now);
    }

    rep(i, n) {
      if (G.at(now).at(i) < mindis.at(i) && G.at(now).at(i) != -1 && color.at(i) != BLACK) {
        mindis.at(i) = G.at(now).at(i);
        color.at(i) = GRAY;
        par.at(i) = now;
      }
    }
  }
  cout << ans << endl;
}

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

const int INF = 1000000000;

int main() {
    int n;
    cin >> n;
    vector< vector<int> > cost(n, vector<int>(n, INF));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int a;
            cin >> a;
            cost[i][j] = (a == -1) ? INF : a;
        }
    }

    vector<int> mincost(n, INF);
    vector<bool> used(n, false);
    mincost[0] = 0;

    int res = 0;
    while (true) {
        int v = -1;
        for (int u = 0; u < n; u++) {
            if (!used[u] && (v == -1 || mincost[u] < mincost[v])) {
                v = u;
            }
        }
        if (v == -1) break;
        used[v] = true;
        res += mincost[v];

        for (int u = 0; u < n; u++) {
            mincost[u] = min(mincost[u], cost[u][v]);
        }
    }

    cout << res << endl;
    return 0;
}