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

# プリムのアルゴリズム
MAX_WEIGHT = 2001
parent = Array.new(n)
visited = Array.new(n, false)
min_weight_adj = Array.new(n, MAX_WEIGHT)

loop do
	vertex = matrix.each_index.
		select { |i| !visited[i] }.
		min_by { |i| min_weight_adj[i] }

	break if vertex.nil?
	visited[vertex] = true

	matrix.each_index do |i|
		next if visited[i] || matrix[vertex][i].nil?

		if matrix[vertex][i] < min_weight_adj[i]
			min_weight_adj[i] = matrix[vertex][i]
			parent[i] = vertex
		end
	end

end

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