while true
    h = gets.to_i
    break if h == 0
    arr = []
    h.times{arr << gets.chomp.split.map(&:to_i)}
    arr = arr.transpose.map{|ar| ar.reverse}
    score = 0
    while true
        change = false
        h.times{|i|
            0.upto(2){|j|
                if arr[j][i]
                    prev, add = arr[j][i], 1
                    while j + add < 5
                        break if arr[j + add][i].nil?
                        break if prev != arr[j + add][i]
                        add += 1
                    end
                    if add >= 3
                        score += prev * add if add >= 3
                        add.times{|k| arr[j + k][i] = nil}
                        change = true
                    end
                end
            }
        }
        break if change == false
        arr.map!{|ar| ar.compact}
    end
    puts score
end

