def check(board)
  flag = true
  board.map! do |line|
    if ret = erase(line)
      flag = false
      ret
    else
      line
    end
  end
  flag
end

def erase(line)
  (1..9).each do |i|
    5.downto(3).each do |j|
      unless (sc = line.scan(/#{i.to_s}{#{j}}/)).empty?
        return line.gsub(sc[0], '0'*j)
      end
    end
  end
  nil
end

def sum(board)
  board.flat_map{ |l| l.split('') }.map(&:to_i).reduce(:+)
end

def fall(board)
  ret = @h.times.map{ '00000' }
  5.times do |i|
    idx = @h - 1
    (@h - 1).downto(0) do |j|
      if board[j][i] != '0'
        ret[idx][i] = board[j][i]
        idx -= 1
      end
    end
  end
  ret
end

def solve(board)
  loop do
    break if check(board)
    board = fall(board)
  end
  sum(board)
end

while (@h = gets.to_i) != 0
  board = @h.times.map{ gets.chomp.delete(' ') }
  score = sum(board)
  puts score - solve(board)
end