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 <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <climits>

#include <vector>
#include <string>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...) do { \
    printf("D(L%d) %s: ", __LINE__, __func__); \
    printf(__VA_ARGS__); \
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG


const int kMaxN = 100;
const int kInvalid = INT_MAX;
int g_weightOfVij[kMaxN][kMaxN];
int g_numOfVertices;


struct Edge {
  Edge (int v0, int v1)
    : v0_(v0)
    , v1_(v1)
  { }

  int v0_;
  int v1_;
  int getWeight() { return g_weightOfVij[v0_][v1_]; }
};


struct VisitData {
  VisitData(int num)
    : numOfData_(num)
    , totalWeight_(0)
    {
      memset(visited_, 0, kMaxN);
    }

  bool visited_[kMaxN];
  int numOfData_;
  int totalWeight_;

  bool isAllVisited();

#if DEBUG
  vector<int> path_; // for history of pathes
#endif // DEBUG
};


bool VisitData::isAllVisited()
{
  for (int i = 0; i < numOfData_; i++)
    if (visited_[i] == false)
      return false;

  return true;
}


void printAllWeights()
{
  DPRINTF("Number of vertices %d\n", g_numOfVertices);

  for (int i = 0; i < g_numOfVertices; i++)
  {
    for (int j = 0; j < g_numOfVertices; j++)
    {
      if (j == 0)  DPRINTF("");
      printf(" %d", g_weightOfVij[i][j]);
    }
    cout << endl;
  }
}


void printAllPath()
{
  DPRINTF("Number of vertices %d\n", g_numOfVertices);
  DPRINTF("$src -> $dst ($weight)\n");

  for (int i = 0; i < g_numOfVertices; i++)
  {
    for (int j = 0; j < g_numOfVertices; j++)
    {
      if (g_weightOfVij[i][j] > 0)
        DPRINTF("%d -> %d (%d)\n", i, j, g_weightOfVij[i][j]);
    }
  }
}


void getConnectedVertices(int pos, vector<int> &vertices)
{
  for (int i = 0; i < g_numOfVertices; i++)
    if (g_weightOfVij[pos][i] > 0)
      vertices.push_back(i);

#if 0
  DPRINTF("connected points from %d :", pos);
  for (int i = 0; i < vertices.size(); i++)
    printf(" %d", vertices[i]);

  cout << endl;
#endif
}


/* find unvisited vertices that are connected from the vertices already visited */
void getNextCandidates(const bool visited[kMaxN], vector<Edge> &candidates)
{
#if 0
  DPRINTF("visited:\n");
  for (int i = 0; i < g_numOfVertices; i++)
    DPRINTF("  [%d] = %d\n", i, visited[i]);
#endif

  for (int startV = 0; startV < g_numOfVertices; startV++)
  {
    if (visited[startV] == true)
    {
      // Get all connected vertices
      vector<int> vertices;
      getConnectedVertices(startV, vertices);

      // Remove vertices that are already visited
      for (int nextV : vertices)
      {
        if (visited[nextV] == false)
          candidates.push_back(Edge(startV, nextV));
      }
    }
  }

#if 0
  DPRINTF("Next candidates:\n");
  for (int i = 0; i < candidates.size(); i++)
    printf("  %d -> %d\n", candidates[i].v0_, candidates[i].v1_);
#endif
}


//int findSpanningTree(int vertex, VisitData info /* Not pointer nor reference */)
int findSpanningTree(int vertex, VisitData &info)
{

  assert(info.visited_[vertex] == false);
  info.visited_[vertex] = true;

#if DEBUG
  info.path_.push_back(vertex);
#endif

#if 1 & DEBUG
  DPRINTF("Path:");
  for (int i = 0; i < info.path_.size(); i++)
    printf(" %d", info.path_[i]);
  cout << endl;
#endif

  if (info.isAllVisited())
  {
    DPRINTF("all vertices visited. total weight %d\n", info.totalWeight_);
    return info.totalWeight_;
  }

  vector<Edge> nextPosCandidates;
  getNextCandidates(info.visited_, nextPosCandidates);

  int minWeight = kInvalid;

  for (int i = 0; i < nextPosCandidates.size(); i++)
  {
    const int startV = nextPosCandidates[i].v0_;
    const int nextV = nextPosCandidates[i].v1_;

    VisitData nextInfo = info;
    nextInfo.totalWeight_ += g_weightOfVij[startV][nextV];

    const int weight = findSpanningTree(nextV, nextInfo);

    if (weight != kInvalid && weight < minWeight)
      minWeight = weight;
  }

  DPRINTF("minWeight %d\n", minWeight);
#if 1 & DEBUG
  DPRINTF("  Path:");
  for (int i = 0; i < info.path_.size(); i++)
    printf(" %d", info.path_[i]);
  cout << endl;
#endif

  return minWeight;
}


int main()
{
  cin >> g_numOfVertices; cin.ignore();

  DPRINTF("Number of vertices %d\n", g_numOfVertices);

  for (int i = 0; i < g_numOfVertices; i++)
  {
    for (int j = 0; j < g_numOfVertices; j++)
      cin >> g_weightOfVij[i][j];

    cin.ignore();
  }

#if DEBUG
  DPRINTF("Number of vertices %d\n", g_numOfVertices);
  printAllWeights();
  //printAllPath();
#endif // DEBUG

  const int startPos = 0;
  VisitData info(g_numOfVertices);

  cout << findSpanningTree(startPos, info) << endl;

  return 0;
}