class Processor
  attr_reader :score

  def initialize()
    @score = 0
    @leastLength = 3
  end

  def process(array)
    ansArray = []
    for i in 0 .. array.length - 1
      ansArray[i] = array[i]
    end

    for i in 0 .. ansArray.length - @leastLength
      if ansArray[i] == nil
        next
      end
      counter = 1
      while (i + counter < ansArray.length) && (ansArray[i] == ansArray[i + counter])
        counter += 1
      end
      if counter >= @leastLength
        for j in 0 .. counter - 1
          @score += ansArray[i + j]
          ansArray[i + j] = nil
        end
      end
    end
    return ansArray
  end
end

class Field
  attr_reader :fieldArray, :continuable, :processor

  def initialize(array)
    @fieldArray = array
    @height = @fieldArray[0].length
    @width = @fieldArray.length
    @processor = Processor.new()
    @continuable = true
  end

  def update()
    for i in 0 .. @height - 1
      rawArray = []
      processedArray = []
      for j in 0 .. @width - 1
        rawArray << @fieldArray[j][i]
      end

      processedArray = @processor.process(rawArray)

      if rawArray != processedArray
        for j in 0 .. @width - 1
          @fieldArray[j][i] = processedArray[j]
        end
      end
    end

    continuable = nil

    for i in 0 .. @width - 1
      continuable |= @fieldArray[i].compact!
    end

    @continuable = continuable
  end

end

while height = gets.to_i
  if height == 0
    break
  end

  inputArray = []

  for i in 0 .. height - 1
    inputArray << gets.chomp.split(" ").map{ |x| x.to_i}
  end

  inputField = Array.new(inputArray[0].length){Array.new(inputArray.length)}

  for i in 0 .. inputArray.length - 1
    for j in 0 .. inputArray[0].length - 1
      inputField[j][inputArray.length - 1 - i] = inputArray[i][j]
    end
  end

  field = Field.new(inputField)
  while field.continuable
    field.update()
  end

  p field.processor.score
end