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: import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    while True:
        H = I()
        if H==0:
            break
        else:
            s = [LI() for _ in range(H)]

            score = 0
            while True:
                # 消滅判定
                score_tmp = 0
                for i in range(H):
                    prev = 0
                    cnt = 1
                    for j in range(5):
                        if s[i][j]==prev:
                            cnt += 1
                        else:
                            if cnt>=3:
                                score_tmp += prev*cnt
                                for k in range(cnt):
                                    s[i][j-cnt+k] = 0
                            prev = s[i][j]
                            cnt = 1
                    if cnt>=3:
                        score_tmp += prev*cnt
                        for k in range(cnt):
                            s[i][j+1-cnt+k] = 0
                # print('s')
                # for i in s:
                #     print(i)
                if score_tmp==0:
                    break
                score += score_tmp

                # 落下処理
                # その石がどれだけ落下するかの算出
                drop = [[0]*5 for _ in range(H)]
                for i in range(5):
                    for j in range(H-1):
                        drop[H-j-2][i] = drop[H-j-1][i] + (1 if s[H-j-1][i]==0 else 0)
                # print('drop')
                # for i in drop:
                #     print(i)
                
                # 落下先にコピー
                for i in range(5):
                    for j in range(H):
                        if drop[H-j-1][i]!=0:
                            s[H-j-1+drop[H-j-1][i]][i] = s[H-j-1][i]
                            s[H-j-1][i] = 0
                # print('dropped s')
                # for i in s:
                #     print(i)

            print(score)

if __name__ == '__main__':
    resolve()

Python: def main(h):
    lst=[list(map(int,input().split())) for _ in range(h)]

    check=1
    sm=0
    while check:
        check=0
        for i in range(h):
            for j in range(3):
                if lst[i][j]==0 : continue
                if lst[i][j+1]!=lst[i][j] : continue
                if lst[i][j+2]!=lst[i][j] : continue
                sm+=3*lst[i][j]
                for k in range(j+3,5):
                    if lst[i][k]==lst[i][j]:
                        lst[i][k]=0
                        sm+=lst[i][j]
                    else : break

                lst[i][j:j+3]=[0,0,0]
                check=1
        for i in range(h-1,0,-1):
            for j in range(5):
                if lst[i][j]==0:
                    for k in range(i-1,-1,-1):
                        if lst[k][j]==0 : continue
                        lst[i][j]=lst[k][j]
                        lst[k][j]=0
                        break
    
    print(sm)
    #print(lst)

while 1:
    h=int(input())
    if h==0 : break
    main(h)

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
						break
					k += 1
					j = k - 1
				if co >= 2:
					re = True
					s += (co+1)*cell
					for l in range(5):
						if l >= p and l <= k:
							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())