Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

Python: def prim():
    global M
    global n
    global INF
    color = ['w' for i in range(n)]
    d = [INF for i in range(n)]
    d[0] = 0
    p = [-1 for i in range(n)]

    while True:
        mincost = INF
        u = -1
        for i in range(n):
            if color[i] != 'b' and d[i] < mincost:
                mincost = d[i]
                u = i
        if u == -1:
            break
        color[u] = 'b'
        for v in range(n):
            if color[v] != 'b' and M[u][v] != INF:
                if M[u][v] < d[v]:
                    d[v] = M[u][v]
                    p[v] = u
                    color[v] = 'g'
    s = 0
    for i in range(n):
        if p[i] != -1:
            s += M[i][p[i]]
    return s


INF = 3000
n = int(input())

M = [[0 for j in range(n)] for i in range(n)]

for i in range(n):
    for j, x in enumerate(list(map(int, input().split()))):
        if x == -1:
            M[i][j] = INF
        else:
            M[i][j] = x
s = prim()
print(s)

Python: def prim():
    INFTY = float('inf')
    color = ['WHITE' for _ in range(n)]
    d = [INFTY for _ in range(n)]
    p = [None for _ in range(n)]
    d[0] = 0
    p[0] = -1

    while True:
        mincost = INFTY
        for i in range(n):
            if color[i] != 'BLACK' and d[i] < mincost:
                mincost = d[i]
                u = i
        
        if mincost == INFTY:
            break

        color[u] = 'BLACK'

        for v in range(n):
            if color[v] != 'BLACK' and A[u][v] != -1:
                if A[u][v] < d[v]:
                    d[v] = A[u][v]
                    p[v] = u
                    color[v] = 'GRAY'
    return sum(d)

n = int(input())
A = []
for _ in range(n):
    l = list(map(int, input().split()))
    A.append(l)

print(prim())

Python: N = int(input())
M = [list(map(int, input().split())) for i in range(N)]
INF = 2001
MAX = 100
d = [INF for i in range(N)]
d[0] = 0
p = [MAX for i in range(N)]
p[0] = -1
mst = [False for i in range(N)]

while True:
  min_w = INF
  for i in range(N):
    if not mst[i] and d[i] < min_w:
      min_w = d[i]
      u = i
  if min_w == INF:
    break
  mst[u] = True
  for v in range(N):
    if not mst[v] and M[u][v] != -1:
      if M[u][v] < d[v]:
        d[v] = M[u][v]
        p[v] = u

print(sum(d))

Python: n = int(input())
adj = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
    tmp = list(map(int, input().split()))
    u = tmp[0]
    u = u -1
    k = tmp[1]
    v = tmp[2:] -1
    for j in range(k):
        adj[u][v[j]] = 1
        
for i in range(n):
        print(" ".join(list(map(str, adj[i]))))

Python: def main():

    N = 100

    n = int(input())
    adj = [[0 for i in range(n)] for j in range(n)]

    for i in range(n):
        tmp = list(map(int, input().split()))
        u = tmp[0]
        u = u -1
        k = tmp[1]
        v = tmp[2:]
        for j in range(k):
            adj[u][v[j]-1] = 1

    for i in range(n):
        for j in range(n):
            print(adj[i][j], end=" ")
        print()
    return 0

if __name__ == "__main__":
    return main()

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int 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;
	color[0] = GRAY;
	
	while( 1 ) {
		minv = INFTY;
		u = -1;
		for(int i = 0; i < n; i++) {
			if(minv > d[i] && color[i] != BLACK) {
				minv = d[i];
				u = 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;
				}
			}
		} 
		
	}
	
	int sum = 0;
	for(int i = 0; i < n; i++) {
		if(p[i] != -1) sum += M[i][p[i]];
	}
	
	return sum;
}

int main() {
	int e; 
	cin >> n;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
		    cin >> e;
		    M[i][j] = (e == -1) ? INFTY : e;
		}
	}
	
	cout << prim() << endl;
	return 0; 
}

C++: #include <cstdio>
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, **M;

int prim() {
    int u, minv;
    int d[n], p[n], color[n];

    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 (d[i] < minv && 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;
                    // printf("d[%d] = %d (parent: %d)\n", v, d[v], u);
                }
            }
        }
    }

    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);

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

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

    return 0;
}