size = STDIN.gets.to_i
matrix = (0...size).inject([]) do |result|
	result << STDIN.gets.split[0...size].map { |v| v == "-1" ? nil : v.to_i}
end

# プリムのアルゴリズム
MAX_WEIGHT = 101
visited   = Array.new(size, false)
parent    = Array.new(size)
min_edges = Array.new(size, MAX_WEIGHT)
loop do
	min_edge = MAX_WEIGHT + 1
	vertex   = -1
	
	# 未訪問で最小の辺を捜す
	size.times do |i|
		if !visited[i] && min_edge > min_edges[i]
			vertex   = i
			min_edge = min_edges[i]
		end
	end
	break if vertex == -1

	visited[vertex] = true
	size.times do |i|
		# 訪問済み or 辺なしは対象外
		if visited[i] || matrix[vertex][i].nil?
			next
		end
		if min_edges[i] > matrix[vertex][i]
			min_edges[i] = matrix[vertex][i]
			parent[i] = vertex
		end
	end
end

puts parent.each_index.
	select { |i| !parent[i].nil? }.
	inject(0) { |r, i| r += matrix[i][parent[i]] }