import scala.annotation.tailrec

object Main extends App {

  import scala.io.StdIn._
  solve
  @tailrec def solve: Unit = {
    val h = readLine.trim.toInt
    if (h == 0) return
    else {
      val state = Array.tabulate(h){_ ⇒ readLine.trim.split(' ').map(_.toInt)}
      var isVanished = true
      var score = 0
      while (isVanished){
        isVanished = false
        for (line ← state){
          line.indices.filter{i ⇒
            line(i) != -1 &&(
            ((i - 2 to i).forall(line.indices.contains(_)) && (i - 2 to i).forall(line(_) == line(i))) ||
              ((i - 1 to i + 1).forall(line.indices.contains(_)) && (i - 1 to i + 1).forall(line(_) == line(i))) ||
                ((i to i + 2).forall(line.indices.contains(_)) && (i to i + 2).forall(line(_) == line(i))))
          }.foreach{i ⇒
            score += line(i)
            line(i) = -1
            isVanished = true
          }
        }
        for (i ← 0 until 5){
          var bottom = h - 1
          for (j ← state.indices.reverse){
            if (state(j)(i) != -1){
              if (state(bottom)(i) == -1) {
                state(bottom)(i) = state(j)(i)
                state(j)(i) = -1
              }
              bottom -= 1
            }
          }
        }
      }
      println(score)
      solve
    }
  }
}

