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
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

while True:
    H = int(input())
    if H == 0:
        break
    B = [0] * H
    for i in range(H):
        B[i] = list(map(int, input().split()))
    W = len(B[i])

    score = 0
    while True:
        # erase step
        for i in range(H):
            start = 0
            while start < W:
                count = 1
                while start + count < W and B[i][start] == B[i][start + count]:
                    count += 1
                if count >= 3:
                    score += B[i][start] * count
                    for j in range(start, start + count):
                        B[i][j] = 0
                start += count
        # drop step
        updated = False
        for i in range(H - 1, -1, -1):
            for j in range(W - 1, -1, -1):
                if B[i][j] != 0:
                    continue
                new_col = []
                for k in range(i, -1, -1):
                    if B[k][j] == 0:
                        continue
                    new_col.append(B[k][j])
                l = 0
                for k in range(i, -1, -1):
                    if l >= len(new_col):
                        B[k][j] = 0
                    else:
                        B[k][j] = new_col[l]
                        updated = True
                    l += 1
        if not updated:
            break
    print(score)

Python: def calc(puzzle,H,score):
    flag=False
    for i in range(H-1,-1,-1):
        stones=[]
        for j in range(5):
            now=puzzle[j][i]
            if (len(stones)==0 or stones[-1]==now) and now!=0: stones.append(now)
            elif len(stones)<3: stones=[now]
            else: break
        if len(stones)>2:
            flag=True
            score+=sum(stones)
            stone=stones[0]
            for j in range(5):
                now=puzzle[j][i]
                if now==stone:
                    if (j<4 and now==puzzle[j+1][i]) or (j>0 and now==puzzle[j-1][i]):
                        puzzle[j].pop(i)
                        puzzle[j].append(0)
    if flag==True:
        return calc(puzzle,H,score)
    else:
        return score

if __name__=="__main__":
    while True:
        score=0
        H=int(input())
        if H==0:
            break
        puzzle=[[] for _ in range(5)]
        for i in range(H):
            temp=list(map(int,input().split()))
            for j in range(5):
                puzzle[j].insert(0,temp[j])
        score=calc(puzzle,H,score)
        print(score)

Python: import sys,queue,math
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in input().split()]
_LI = lambda : [int(x)-1 for x in input().split()]

while True:
    N = int(input())
    if N == 0 : break

    puyo = [[int(x) for x in input().split()] + [0] for _ in range(N)]
    p = 0
    update = True
    while update:
        update = False
        for i in range(N):
            cl = puyo[i][0]
            cnt = cl > 0
            for j in range(1,6):
                if puyo[i][j] == cl and cl > 0:
                    cnt += 1
                else:
                    if cnt >= 3:
                        p += cl * cnt
                        for k in range(cnt):
                            puyo[i][j-k-1] = 0
                        update = True
                    cl = puyo[i][j]
                    cnt = cl > 0
        for i in range(N-1,0,-1):
            for j in range(5):
                if puyo[i][j] > 0: continue
                x = i
                while x > 0:
                    x -= 1
                    if puyo[x][j] > 0:
                        puyo[i][j] = puyo[x][j]
                        puyo[x][j] = 0
                        break
    print (p)