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;

#define MAX 101
#define BLACK 0
#define GRAY 1
#define WHITE 2
#define INF (1<<30)

int graph[MAX][MAX];
int n;

// プロトタイプ宣言
int prim();
// ここまで

int main(){
	cin>>n;

	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>graph[i][j];
		}
	}
	cout<<prim()<<endl;


	return 0;
}




//---------------------- 
int prim(){
	int from;
	int color[MAX];
	int d[MAX];
	int p[MAX];

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

	while(1){
		int minCost=INF;
		from=-1;

		for(int i=0;i<n;i++){
			if(color[i]!=BLACK && d[i]<minCost){
				minCost=d[i];
				from=i;
			}
		}
		if(from == -1) break;

		color[from]=BLACK;
		for(int to=0;to<n;to++){
			if(color[to]!=BLACK && graph[from][to]!=-1){
				if(graph[from][to]<d[to]){
					d[to]=graph[from][to];
					p[to]=from;
					color[to]=GRAY;
				}
			}
		}
	}

	int sum=0;
	for(int i=0;i<n;i++){
		// if(p[i]!=-1){
		// 	sum += graph[i][ p[i] ];
		// }
		if(d[i]!=INF){
			sum+=d[i];
		}
	}

	return sum;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstring>
#include <queue>
#include <string>

using namespace std;

const double PI = 3.14159265358979;

int G[101][101];
int visited[101];
int n;

int houxuan[101];
int father[101];

int prim() {

	for (int i = 0; i < n; i++)	{
		houxuan[i] = INT_MAX;
		father[i] = -1;
	}
	houxuan[0] = 0;

	int sum = 0;
	while (true) {

		int minx = INT_MAX;
		int u = -1;
		for (int i = 0; i < n; i++) {

			if (!visited[i] && houxuan[i] < minx) {
				minx = houxuan[i];
				u = i;
			}
		}
		if (u == -1) break;

		visited[u] = 1;
		sum += minx;

		for (int i = 0; i < n; i++) {
			if (!visited[i] && G[u][i] != -1) {
				if (houxuan[i] > G[u][i]) {
					houxuan[i] = G[u][i];
					father[i] = u;
				}
			}
		}
	}	

	return sum;
}

int main() {
	
	
	cin >> n;

	for (int i = 0; i < n; i++)	{
		for (int j = 0; j < n; j++)	{
			cin >> G[i][j];
		}
	}
	
	cout << prim() << endl;

	return 0;
}

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

const int MAX = 100;
const int INF = 1 << 29;

int n;
int grid[MAX][MAX];

int prim()
{
	int ans[MAX];
	int visited[MAX];

	for (int i = 0; i < n; i++) {
		ans[i] = INF;
		visited[i] = false;
	}

	ans[0] = 0;

	while (true) {
		int min = INF;
		int min_index;

		for (int i = 0; i < n; i++) {
			if (min > ans[i] && visited[i] == false) {
				min = ans[i];
				min_index = i;
			}
		}

		if (min == INF) {
			break;
		}

		visited[min_index] = true;

		for (int i = 0; i < n; i++) {
			if (ans[i] > grid[min_index][i] && visited[i] == false) {
				ans[i] = grid[min_index][i];
			}
		}
	}

	int sum = 0;

	for (int i = 0; i < n; i++) {
		sum += ans[i];
	}

	return sum;
}

int main()
{
	cin >> n;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int temp;
			cin >> temp;

			if (temp == -1) {
				grid[i][j] = INF;
			}
			else {
				grid[i][j] = temp;
			}
		}
	}

	cout << prim() << endl;

	return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
const int MAX_N =200;
int n;
int cost[MAX_N][MAX_N];
bool visit[MAX_N];
int mincost[MAX_N]={INF};
int prime(){
	//memset(mincost, INF, sizeof(mincost));
	memset(visit, false, sizeof(visit));
	int res = 0;
	mincost[0]=0;
	/*rep(i, n){
		cout <<mincost[i];
		cout <<endl;
	}
	cout <<endl;*/
	while(1){
		int v = -1;
		for(int u =0; u <n;u++){
			if(!visit[u]&&(v==-1||mincost[u] < mincost[v])){
				v =u;
			}
		}
		if(v==-1)break;
		//cout <<"v = "<<v<<endl;
		visit[v]=true;
		res +=mincost[v];
		//cout <<res<<endl;
		for(int u =0;u <n ;u++){
			mincost[u]=min(mincost[u],cost[v][u]);
		}
	}
	//cout <<res<<endl;
	return res;
}

int main(){
	cin>>n;
	for(int i =0;i < n;i++){
		mincost[i]=INF;
		for(int j =0;j < n;j++){
			int x;
			cin>>x;
			if(x==-1){
				cost[i][j]=INF;
			}else{
				cost[i][j]=x;
			}
		}
	}
	/*rep(i, n){
		rep(j, n){
			printf("%10d",cost[i][j]);
		}
		cout <<endl;
	}*/

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