while true
    h = gets.to_i
    exit if h==0
    as = h.times.map{gets.split.map(&:to_i)}
    score = 0
    while true
        streak = false
        h.times do |i|
            erased = false
            5.downto(3) do |l|
                if !erased
                    j=0
                    as[i].each_cons(l) do |es|
                        if es.uniq.size==1 && es[0]!=false
                            score+=es[0]*l
                            l.times do |k|
                                as[i][j+k] = false
                            end
                            erased = true
                            streak = true
                            break
                        end
                        j+=1
                    end
                end
            end
        end
        if !streak
            puts score
            break
        end
        
        5.times do |i|
            h.times do |j|
                if as[j][i]==false
                    j.downto(1) do |k|
                        as[k][i]=as[k-1][i]
                    end
                    as[0][i] = false
                end
            end
        end
    end
end
