loop do
  h = gets.chomp.to_i

  if h == 0
    break
  end
  
  cells = Array.new(h)

  h.times do |i|
    cells[i] = gets.chomp.split(" ").map(&:to_i)
  end

  ans = 0
  loop do
    flg = false

    for i in 0...h
      for j in 0...5
        if cells[i][j] == -1
          next
        end
        cnt = 0
        for k in j...5
          if cells[i][j] != cells[i][k]
            break
          end
          cnt += 1
        end
        if cnt < 3
          next
        end
        ans += cells[i][j] * cnt
        for k in 0...cnt
          cells[i][j + k] = -1
        end
        flg = true
      end
    end
    
    if !flg
      break
    end

    for j in 0...5
      for i in 0...h
        idx = h - i - 1
        if cells[idx][j] == -1
          next
        end
        while idx + 1 < h && cells[idx + 1][j] == -1 do
          cells[idx][j], cells[idx + 1][j] = cells[idx + 1][j], cells[idx][j]
          idx += 1
        end
      end
    end
  end

  puts ans
end
  