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 solve():
    cnt = -1
    while 1:
        cnt += 1
        H = int(input())
        if H==0:
            break
        puzzle = [list(map(int, input().split())) for _ in range(H)]
        ans = 0
        while True:
            flg = True
            for i, row in enumerate(puzzle):
                if len(set(row))>=4:
                    continue
                streak = [-1, 1]
                for j, c in enumerate(row):
                    if c==-1:
                        streak[0] = -1
                        streak[1] = 1
                        continue
                    if c==streak[0]:
                        streak[1]+=1
                        if streak[1]==3:
                            flg = False
                            ans += c*3
                            puzzle[i][j-2:j+1] = [-1]*3
                        elif streak[1]>=4:
                            ans += c
                            puzzle[i][j] = -1
                    else:
                        streak[0] = c
                        streak[1] = 1
            for i, row in enumerate(puzzle[::-1]):
                if i==H-1:
                    break
                for j, c in enumerate(row):
                    if c==-1:
                        for h in range(H-i-1):
                            if puzzle[(H-i-1)-h-1][j]!=-1:
                                puzzle[-i-1][j] = puzzle[(H-i-1)-h-1][j]
                                puzzle[(H-i-1)-h-1][j] = -1
                                break
            if flg:
                print(ans)
                break

solve()

Python: def dis():
    k=0
    for i in range(n):
        for j in range(3):
            if l[i][j]==0:
                continue
            re=1
            for z in range(1,100):
                if j+z>4:break
                if l[i][j]==l[i][j+z]:
                    re+=1
                else: break
            if re<3:continue
            k+=l[i][j]*re
            for x in range(re):
                l[i][j+x]=0
    return k
def dow():
    for i in range(n-1,0,-1):
        for j in range(4,-1,-1):
            if l[i][j]==0:
                for z in range(1,i+1):
                    if l[i-z][j]!=0:
                        l[i][j],l[i-z][j]=l[i-z][j],l[i][j]
                        break
while True:
    n=int(input())
    if n==0:exit()
    l=[list(map(int,input().split())) for _ in range(n)]
    ans=0
    k=dis()
    while k!=0:
        dow()
        ans+=k
        k=dis()
    print(ans)

Python: ans_list = []

while True:
    h = int(input())
    if not h:
        break

    stone = [list(map(int, input().split())) for i  in range(h)]
    ans = 0
    while True:
        key = 1
        for i in range(h):
            del_list = []
            for j in range(len(stone[0]) - 2):
                if stone[i][j:j + 3] == [stone[i][j], stone[i][j], stone[i][j]] and stone[i][j] != 0:
                    key = 0
                    del_list.append(j)
            for j in del_list:
                ans += sum(stone[i][j:j + 3])
                stone[i][j:j + 3] = [0, 0, 0]

        key2 = 1
        ij = 0
        while key2:
            import copy
            last_stone = copy.deepcopy(stone)
            key2 = 0
            for i in range(h - 1, -1, -1):
                for j in range(len(stone[i])):
                    if stone[i][j] == 0:
                        if i != 0:
                            key2 = 1
                            stone[i][j] = stone[i - 1][j]
                            stone[i - 1][j] = 0
            ij += 1
            if last_stone == stone:
                key2 = 0

        if key:
            break
    ans_list.append(ans)

for i in ans_list:
    print(i)

Python: while True:
    score = 0
    h = int(input())
    if h == 0: break
    w = 5

    field = [list(map(int, input().split())) for x in range(h)]

    def delete(field):
        flg = True
        for row in field:
            target = None
            cnt = 0
            fs = []
            f = None
            t = None
            for i, cell in enumerate(row):
                if target != None and cell == target:
                    cnt += 1
                else:
                    if cnt >= 2:
                        f = fs[-1]
                        t = i - 1
                    fs.append(i)
                    target = cell
                    cnt = 0
            if cnt >= 2:
                f = fs[-1]
                t = i

            if f != None:
                global score
                score += (t - f + 1) * row[f]
                for i in range(f, t + 1):
                    row[i] = None
                flg = False
        if flg: return flg
        return field


    def drop(field):
        for y, row in enumerate(field[:-1]):
            for x, cell in enumerate(row):
                if field[y + 1][x] == None:
                    for n in range(y + 1, 0, -1):
                        field[n][x] = field[n - 1][x]
                    else:
                        field[0][x] = None
        return field

    while True:
        field = delete(field)
        if field == True:
            break
        field = drop(field)
        # for x in field: print(*map(lambda x: "X" if x == None else x, x))
        # print("===========")
    print(score)