Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		h := io.NextInt()
		if h == 0 {
			break
		}
		p := make([][]int, h)
		for i := 0; i < h; i++ {
			p[i] = make([]int, 5)
			for j := 0; j < 5; j++ {
				p[i][j] = io.NextInt()
			}
		}
		ans := 0
		for {
			sc := puyo(p, h)
			if sc == 0 {
				break
			}
			fall(p, h)
			ans += sc
		}
		io.Println(ans)
	}
}

func puyo(p [][]int, h int) int {
	sc := 0
	for i := 0; i < h; i++ {
		for j := 0; j < 3; j++ {
			var cnt int
			for cnt = 1; j+cnt < 5; cnt++ {
				if p[i][j] != p[i][j+cnt] {
					break
				}
			}
			if cnt > 2 {
				sc += p[i][j]*cnt
				for k := 0; k < cnt; k++ {
					p[i][j+k] = 0
				}
			}
		}
	}
	return sc
}

func fall(p [][]int, h int) {
	for j := 0; j < 5; j++ {
		for k := 1; k < h; k++ {
			for i := h - 1; i >= k; i-- {
				if p[i][j] == 0 {
					p[i][j] = p[i-1][j]
					p[i-1][j] = 0
				}
			}
		}
	}
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

Python: while True:
    H = int(input())
    if H == 0:
        break
    squares = [list(map(int, input().split())) for _ in range(H)]
    score = 0
    while True:
        deleted = False
        for i in range(H):
            for j in range(5, 2, -1):
                for k in range(5 - j + 1):
                    deletable = True
                    first_num = squares[i][k]
                    if first_num == 0:
                        continue
                    for num in squares[i][k + 1:k + j]:
                        if first_num != num:
                            deletable = False
                            break
                    if deletable:
                        score += first_num * j
                        squares[i][k:k + j] = [0] * j
                        deleted = True
        # print(squares)
        if deleted:
            while True:
                not_falled = True
                # 下からやっていった方がいい。また、0行目はやる必要なし
                for i in range(H - 1, 0, -1):
                    for k, num in enumerate(squares[i]):
                        if num == 0 and squares[i - 1][k] != 0:
                            squares[i - 1][k], squares[i][k] = squares[i][k], squares[i - 1][k]
                            not_falled = False
                if not_falled:
                    break
        else:
            break
    print(score)
    # print(squares)

Python: while 1:
    n=int(input())
    if n==0:break
    A=[list(map(int,input().split())) for _ in range(n)]
    ans=0
    while 1:
        score=ans
        for i in range(n):
            cnt,num=0,A[i][0]
            for j in range(5):
                if A[i][j]==num:cnt +=1
                else:
                    if cnt>=3:
                        ans +=num*cnt
                        for k in range(cnt):A[i][j-k-1]=0
                    num=A[i][j]
                    cnt=1
                if j==4 and cnt>=3:
                    ans +=num*cnt
                    for k in range(cnt):A[i][j-k]=0
        B=[[-1 for _ in range(5)] for _ in range(n)]
        for i in range(5):
            cnt=n-1
            for j in range(n)[::-1]:
                if A[j][i]!=0:
                    B[j][i]=cnt
                    cnt -=1
        C=[[0 for _ in range(5)] for _ in range(n)]
        for i in range(5):
            for j in range(n):
                if B[j][i]!=-1:
                    C[B[j][i]][i]=A[j][i]
        A=C
        if score==ans:break
    print(ans)

Python: while True:
  h = int(input())
  if h==0:
    break
  board = [list(map(int, input().split())) for i in range(h)]
  ans = 0

  while True:
    tans = 0
    for y in range(h):
      begin, end = 0, 0
      for x in range(1, 5):
        if board[y][x]==board[y][x-1]:
          end = x
        elif end-begin+1>=3:
          break
        else:
          begin = x
          continue
      if end-begin+1>=3:
        tans += board[y][begin]*(end-begin+1)
        for x in range(begin, end+1):
          board[y][x] = 0

    move = True
    while move:
      move = False
      for y in range(1, h):
        for x in range(5):
          if board[y][x]==0 and board[y-1][x]==0:
            continue
          elif board[y][x]==0:
            board[y][x], board[y-1][x] = board[y-1][x], board[y][x]
            move = True
    if tans>0:
      ans += tans
    else:
      break
  print(ans)

Python: from itertools import zip_longest

while True:
    n = int(input())
    if not n:
        break
    field = [list(map(int, input().split())) for _ in range(n)]
    field.reverse()
    score = 0
    while True:
        no_more_disappear = True

        for y, row in enumerate(field):
            for i, stone in enumerate(row[:3]):
                if stone is None:
                    continue
                cnt = 1
                for stone2 in row[i + 1:]:
                    if stone != stone2:
                        break
                    cnt += 1
                if cnt >= 3:
                    row[i:i + cnt] = [None] * cnt
                    score += stone * cnt
                    no_more_disappear = False

        if no_more_disappear:
            break

        fieldT = map(lambda row: filter(None, row), zip(*field))
        field = list(map(list, zip_longest(*fieldT)))

    print(score)

Python: a = int(input())
global s
s = 0

def de(ppp,h):
	re = False
	co  = 0
	global s
	for lo in ppp:
		for j ,cell in enumerate(lo):
			k = j
			p = j
			if k <= 2:
				while cell == lo[k+1] and cell != None:
					co += 1
					if k == 3:
						j = k+1
						break
					k += 1
					j = k
				if co >= 2:
					re = True
					s += (co+1)*cell
					for l in range(5):
						if l >= p and l <= j:
							lo[l] = None
				co = 0

	return re



def mo(ppp,h):
	for i ,roe in enumerate(ppp):
		for j , cell in enumerate(roe):
			kkk = i
			while i != 0 and ppp[kkk-1][j] == None:
				ppp[kkk-1][j] = ppp[kkk][j]
				ppp[kkk][j] = None
				if kkk == 1:
					break
				kkk -= 1


while a != 0:
	ppp = [list(map(int,input().split())) for i in range(a)]
	ppp.reverse()
	while(de(ppp,a) == True):
		mo(ppp,a)
	print(s)

	s = 0

	a = int(input())