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:
    n = int(input())
    if n == 0:
        break
    
    field = [input().split() for _ in range(n)]
    field = list(reversed(field))
    score = 0

    while True:

        f = True

        for row in field:
            clear_index = set()
            for i in range(3):
                if row[i] == -1:
                    continue
                if row[i] == row[i+1] and row[i+1] == row[i+2]:
                    f = False
                    clear_index.add(i)
                    clear_index.add(i+1)
                    clear_index.add(i+2)

            for i in clear_index:
                score += int(row[i])
                row[i] = -1
        
        if f:
            break

        for i,row in enumerate(field):
            for j in range(5):
                if row[j] != -1:
                    continue
                
                for k in range(i+1,n):
                    if field[k][j] != -1:
                        field[k][j],row[j] = row[j],field[k][j]
                        break
    print(score)

Python: from functools import lru_cache

digits = list(map(lambda x: str(x),range(1,10)))

@lru_cache(maxsize=None) 
def check(s,n):  
    return ' '.join([s]*n)

def down(x):
    x = list(x[::-1])   
    while '0' in x:
        x.remove('0')
        x.append('#')
    return x

def main(n):
    data = '\n'.join(map(lambda x: input(), range(n)))  
    score = 0
    while True:
        removed = False
        for d in digits:
            if check(d, 3) in data:
                for i in range(5, 2, -1):
                    if check(d, i) in data:
                        data = data.replace(check(d, i), check('0', i))
                        removed = True
                        score += int(d) * i
                        break
        if removed == False:
            break
        data = zip(*map(lambda x: x.split(), data.split('\n')))
        data = map(lambda x: down(x), data)
        data = list(map(lambda x: ' '.join(x),zip(*data)))
        data = '\n'.join(data[::-1])
    print(score)    

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

Python: while True:
    N = int(input())
    if not N: break
    cells = [[int(x) for x in input().split()] for _ in range(N)]
    point = 0
    while True:
        flag = False
        for i, cell in enumerate(cells):
            j = 0
            while j < 5:
                s, color = j, cell[j]
                while j < 5 and cell[j] == color:
                    j += 1
                if color != 0 and j - s >= 3:
                    point += color * (j - s)
                    cell[s:j] = [0] * (j-s)
                    flag = True
        for i in range(5):
            for j in reversed(range(N)):
                s = j
                while s >= 0 and not cells[s][i]:
                    s -= 1
                if s >= 0:
                    cells[j][i], cells[s][i] = cells[s][i], cells[j][i]
        if not flag:
            break
    print(point)

Python: def fall(S,H):
    for h in range(H-1):
        for i in range(5):
            if S[h][i]=="0":
                S[h][i] = S[h+1][i]
                S[h+1][i] = "0"
    return S
while True:
    H = int(input())
    if H==0:
        break
    S = [list(input().split()) for i in range(H)]
    S = S[::-1]
    ans = 0
    k = 1
    while k!=0:
        k = 0
        for h in range(H):
            s = S[h]
            for l in range(3):
                for r in range(5,2+l,-1):
                    if s[l]=="0":
                        continue
                    if s[l:r].count(s[l])==(r-l):
                        k = 1
                        ans += int(s[l])*(r-l)
                        S[h][l:r] = ["0"]*(r-l)
        for i in range(H):
            S = fall(S,H)
    print(ans)

Python: from inspect import currentframe


def debug_print(s):
    # print(s)
    return


def debug_key(*args):
    names = {id(v): k for k, v in currentframe().f_back.f_locals.items()}
    debug_print(', '.join(names.get(id(arg), '???')+' = '+repr(arg) for arg in args))


def solve(H, stone):
    debug_print("\n-----solve-----")
    debug_key(stone)

    ans = -1
    tmp = 1
    while tmp:
        ans += tmp
        tmp = 0
        erase = [set() for _ in range(5)]
        for i in range(H):
            for j in range(1, 4):
                if stone[i][j - 1] == stone[i][j] == stone[i][j + 1]:
                    for k in range(j - 1, j + 2):
                        erase[k].add(i)
        stone2 = list(map(list, zip(*stone)))
        for j in range(5):
            for i in sorted(erase[j], reverse=True):
                tmp += stone[i][j]
                del stone2[j][i]
            stone2[j] = [0] * len(erase[j]) + stone2[j]
        stone = list(map(list, zip(*stone2)))
    print(ans)

    return


if __name__ == '__main__':

    while True:
        H_input = int(input())
        if H_input == 0:
            break
        stone_input = [list(map(int, input().split())) for _ in range(H_input)]
        solve(H_input, stone_input)