### constant

MAX_X = 5

### main

loop do
  h = gets.to_i
  break if h == 0

  cells = MAX_X.times.map{[]}
  h.times do
    line = gets.split.map(&:to_i)
    for i in (0...MAX_X)
      cells[i].unshift(line[i])
    end
  end
  #p cells

  total = 0
  loop do
    changed = false
    for y in (0...h)
      max_len = max_c = max_x = 0
      x = 0
      while x < MAX_X
        if cells[x][y] <= 0
          x += 1
          next
        end
        c = cells[x][y]
        len = 1
        while x + len < MAX_X &&  cells[x + len][y] == c
          len += 1
        end
        if max_len < len
          max_len = len
          max_c = c
          max_x = x
        end
        x += len
      end
      #p [max_len, max_c, max_x]

      if max_len >= 3
        changed = true
        total += max_len * max_c
        for l in (0...max_len)
          cells[max_x + l][y] = 0
        end
      end
    end
    break if ! changed

    for x in (0...MAX_X)
      while ! (y = cells[x].index(0)).nil?
        cells[x].delete_at(y)
        cells[x] << -1
      end
    end
    #p cells
  end

  puts total
end