Go: package main

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

// Define disjoint-set forests

type vertexNo int

type vertex struct {
	number vertexNo
	rank int
	parent *vertex
}

func makeSet(number vertexNo) vertex {
	return vertex {
		number: number,
		rank: 0,
		parent: nil,
	}
}

func union(x, y *vertex) *vertex {
	return link(findSet(x), findSet(y))
}

func link(x, y *vertex) *vertex {
	if x.rank > y.rank {
		y.parent = x
		return x
	} else {
		x.parent = y
		if x.rank == y.rank {
			y.rank += 1
		}
		return y
	}
}

func findSet(v *vertex) *vertex {
	if v.parent == nil {
		return v
	}
	v.parent = findSet(v.parent)
	return v.parent
}

// Function to calculate minimum spanning tree

type matrix [][]int

type edge struct {
	one vertexNo
	two vertexNo
	weight int
}

const(
	MAX_VERTICES = 100
)

func newEdgesFromMatrix(matrix matrix) []edge {
	edges := make([]edge, 0, MAX_VERTICES * MAX_VERTICES)

	for i := 0; i < len(matrix); i++ {
		row := matrix[i]
		for j := i; j < len(row); j++ {
			if row[j] >= 0 {
				edge := edge{one: vertexNo(i+1), two: vertexNo(j+1), weight: row[j]}
				edges = append(edges, edge)
			}
		}
	}

	return edges
}

func solve(matrix matrix) int {
	nVertices := len(matrix)

	// initialize vertices
	vertices := make([]vertex, nVertices)
	for i := 0; i < nVertices; i++ {
		vertices[i] = makeSet(vertexNo(i+1))
	}

	// prepare edges sorted in the ascending order of weight
	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	unionCount := 0
	weightSum := 0
	for unionCount < nVertices-1 {
		for i := 0; i < len(edges); i++ {
			edge := edges[i]
			v1 := &vertices[edge.one-1]
			v2 := &vertices[edge.two-1]
			rep1 := findSet(v1)
			rep2 := findSet(v2)
			if rep1.number == rep2.number {
				continue
			}
			union(rep1, rep2)
			edges = append(edges[0:i], edges[i+1:]...)
			weightSum += edge.weight
			unionCount++
			break
		}
	}

	return weightSum
}

// main

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	matrix := parseMatrix(n, sc)

	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	fmt.Println(solve(matrix))
}

func parseMatrix(n int, sc *bufio.Scanner) [][]int {
	matrix := make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			matrix[i][j] = x
		}
	}
	return matrix
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

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