
while (h = gets.to_i) > 0
    field = h.times.map{gets.split.map(&:to_i)}
    
    score = 0
    continue = true
    while (continue)
        continue = false
        no = nil
        cnt = 0
        field.map! do |line|
            new_line = line.clone
            line.each_with_index do |e,x|
                no,cnt = e,0 if no != e
                cnt += 1
                if cnt >= 3 && no != 0
                    3.times do |j|
                        break if new_line[x-j] == 0
                        score += new_line[x-j].to_i
                        new_line[x-j] = 0
                        continue = true
                    end
                end
            end
            new_line
        end
        
        (h-2).downto(0) do |y|
            field[y].each_index do |x|
                0.upto(h-2-y) do |i|
                    break if field[y+i][x] == 0 || field[y+i+1][x] != 0
                    field[y+i+1][x], field[y+i][x] = field[y+i][x], 0
                end
            end
        end
    end
    p score
    
end
