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: def CDP(h,w,k):
  q=[]
  ans=0
  d=-1
  m=0
  while True:
    if d==ans:break
    d=ans
    for i in range(h):
      cnt=0
      for j in range(w-1):
        if a[j][i]==a[j+1][i]:cnt+=1
        else:
          if cnt>=k-1:
            q.append((j-cnt,i,cnt+1))
          cnt=0
      if cnt>=k-1:
        q.append((w-cnt-1,i,cnt+1))      
    while q:
      j,i,cnt=q.pop()
      for c in range(cnt):
        ans+=a[j+c].pop(i)
        a[j+c].append(0)
  return ans

while True:
  h=int(input())
  if h==0:break
  b=[list(i[::-1]) for i in zip(*[input().split() for _ in range(h)])]
  a=[[int(s) for s in strs] for strs in b]
  print(CDP(h,5,3))

Python: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1193&lang=jp
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    ans = []
    while True:
        H = int(readline())
        if H == 0:
            break

        c = [[int(i) for i in readline().split()] for _ in range(H)]
        seq = [[1] for _ in range(H)]
        ans_tmp = [0]
        
        score = 0
        while True:
            # 消す
            for i, line in enumerate(c):
                for j in range(1,5):
                    if line[j] == line[j-1]:
                        seq[i][-1] += 1
                    else:
                        seq[i].append(1)
            
                acc = -1
                for s in seq[i]:
                    acc += s
                    if s >= 3:
                        score += s * line[acc]
                        for j in range(acc - s + 1, acc + 1):
                            line[j] = 0
                seq[i] = [1]

            for i in reversed(range(H)):
                for j in range(5):
                    if c[i][j] == 0:
                        for h in reversed(range(i)):
                            if c[h][j] != 0:
                                c[h][j], c[i][j] = c[i][j], c[h][j]
                                break

            ans_tmp.append(score)
            if ans_tmp[-1] == ans_tmp[-2]:
                ans.append(ans_tmp[-1])
                break

    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

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+2:
							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(ppp)
	print(s)

	s = 0

	a = int(input())

Python: def main(n):
    f = [list(map(int, input().split())) for i in range(n)]
    d = True
    ans = 0
    while d:
        d = False
        for i in range(n):
            l = []
            for k in range(3):
                if f[i][k] == 0:
                    continue
                if f[i][k] == f[i][k + 1] == f[i][k + 2]:
                    l.append(k)
                    l.append(k + 1)
                    l.append(k + 2)
            if l == []:
                continue
            l = list(set(l))
            b = f[i][l[0]]
            d = True
            for k in l:
                ans += b
                f[i][k] = 0
        if d:
            for i in range(n-1,-1,-1):
                for k in range(5):
                    x = i
                    while f[x][k]:
                        if x < n - 1:
                            if f[x + 1][k] == 0:
                                f[x+1][k] = f[x][k]
                                f[x][k] = 0
                                x += 1
                                continue
                        break
    print(ans)

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

Python: while True:
    h = int(input())
    if h == 0:
        break
    arr = [[-1 for i in range(h)], [-1 for i in range(h)], [-1 for i in range(h)], [-1 for i in range(h)], [-1 for i in range(h)]]
    midlen = h
    for i in range(h - 1, -1, -1):
        inp = list(map(int, input().split()))
        for j, e in enumerate(inp):
            arr[j][i] = e
    
    score = 0
    za = True
    while za:
        za = False
        for i, e in list(enumerate(arr[2]))[::-1]:
            for j in range(3):
                ge = 0
                for h in range(j, 5):
                    if len(arr[h]) > i:
                        if arr[h][i] == e:
                            ge += 1
                            continue
                    break
                if ge > 2:
                    za = True
                    for h in range(j, j + ge):
                        score += arr[h].pop(i)
    print(score)