using System;
using System.Linq;

namespace ALDS1_12_A
{
    class Program
    {
        private static int n;
        private static int[,] neigborWeights;
        private static int[] d;

        private static bool[] isVisited;
        static void Main(string[] args)
        {
            Program.n = int.Parse(Console.ReadLine());
            Program.d = new int[Program.n];
            Program.isVisited = new bool[Program.n];
            Program.neigborWeights = new int[Program.n, Program.n];
            for (int i = 0; i < Program.n; i++)
            {
                int[] tmpLine = Console.ReadLine()
                                        .Trim(' ')
                                        .Split(' ')
                                        .Select(s => int.Parse(s))
                                        .ToArray();
                for (int j = 0; j < Program.n; j++)
                {
                    Program.neigborWeights[i, j] = tmpLine[j];
                }
                Program.d[i] = (int)Math.Pow(10, 7);
            }
            Program.d[0] = 0;

            int minSumWeight = 0;
            MinHeap minHeap = new MinHeap(Program.n);
            while(minHeap.maxIndex >= 0)
            {
                int vertex = minHeap.Dequeue(Program.d);
                Program.isVisited[vertex] = true;
                minSumWeight += d[vertex];
                for (int i = 0; i < Program.n; i++)
                {
                    if
                    (   Program.neigborWeights[vertex, i] >= 0
                    &&  !Program.isVisited[i]
                    &&  Program.d[i] > Program.neigborWeights[vertex, i]
                    )
                    {
                        Program.d[i] = Program.neigborWeights[vertex, i];
                        minHeap.UpHeapfy(i, Program.d);
                    }
                }
            }
            Console.WriteLine(minSumWeight);
        }
    }
    class MinHeap
    {
        private int[] heap;
        private int[] vertexIndex;
        public int maxIndex;
        public MinHeap(int heapSize)
        {
            this.heap = new int[heapSize];
            this.vertexIndex = new int[heapSize];
            for (int i = 0; i < heapSize; i++)
            {
                this.heap[i] = i;
                this.vertexIndex[i] = i;
            }
            this.maxIndex = heapSize - 1;
        }
        
        public int Dequeue(int[] d)
        {
            int returnVal = this.heap[0];
            this.vertexIndex[this.heap[0]] = -1;
            this.heap[0] = this.heap[this.maxIndex];
            this.vertexIndex[this.heap[this.maxIndex]] = 0;
            this.maxIndex--;
            this.DownHeapfy(this.heap[0], d);
            return returnVal;
        }
        public void DownHeapfy(int vertex, int[] d)
        {
            int tmpIndex = this.vertexIndex[vertex];
            int leftIndex = (tmpIndex + 1) * 2 - 1;
            int rightIndex = (tmpIndex + 1) * 2;
            if (leftIndex <= this.maxIndex && d[this.heap[leftIndex]] < d[this.heap[tmpIndex]])
            {
                tmpIndex = leftIndex;
            }
            if (rightIndex <= this.maxIndex && d[this.heap[rightIndex]] < d[this.heap[tmpIndex]])
            {
                tmpIndex = rightIndex;
            }
            if (tmpIndex != this.vertexIndex[vertex])
            {
                int parent = vertex;
                int parentIndex = this.vertexIndex[parent];
                int tmp = this.heap[tmpIndex];
                
                this.heap[parentIndex] = tmp;
                this.heap[tmpIndex] = parent;

                this.vertexIndex[parent] = tmpIndex;
                this.vertexIndex[tmp] = parentIndex;

                this.DownHeapfy(vertex, d);
            }
            return;
        }

        public void UpHeapfy(int vertex, int[] d)
        {
            int vertexIndex = this.vertexIndex[vertex];
            if (vertexIndex == 0)
            {
                return;
            }
            int parentIndex = (vertexIndex + 1) / 2 - 1;
            int parent = this.heap[parentIndex];
            if (d[vertex] < d[parent])
            {
                this.heap[vertexIndex] = parent;
                this.vertexIndex[vertex] = parentIndex;

                this.heap[parentIndex] = vertex;
                this.vertexIndex[parent] = vertexIndex;

                this.UpHeapfy(vertex, d);
            }
            return;
        }
    }
}



Measured memory_bytes: 18352