def check(row)
    return [[],0] if !row || row[2] == 0
    ret = []
    if row[2] == row[1]
        if row[1] == row[0]
            ret.concat([0,1,2])
        end
        if row[2] == row[3]
            ret.concat([1,2,3])
        end
    end
    if row[2] == row[3]
        if row[3] == row[4]
            ret.concat([2,3,4])
        end
        if row[1] == row[2]
            ret.concat([1,2,3])
        end
    end
    ret = ret.uniq
    score = ret.map{|i| row[i]}.inject(:+) || 0
    return [ret, score]
end
def pp(f)
    puts
    f.transpose.reverse.each do |r|
        r.each do |c|
            print c.to_s + " "
        end
        puts
    end
end

loop do
    h = gets.to_i
    break if h == 0
    f = h.times.map{gets.split.map(&:to_i)}
    f = f.reverse.transpose
    score = 0
    b = true
    while b
        b = false
        ds = Array.new(h)
        0.upto(h) do |i|
            ds[i], p = check f.transpose[i]
            if p
                score += p
                b = true if p > 0
            end
        end
        ds.reverse.each_with_index do |dd,i|
            dd.each do |d|
                f[d].delete_at(h-i)
                f[d].push(0)
            end
        end
        break if !b
    end
    puts score
end