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 math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        h = I()
        if h == 0:
            break

        a = [LI() for _ in range(h)]
        f = True
        r = 0
        while f:
            f = False
            d = [[0]*5 for _ in range(h)]
            for i in range(h):
                for j in range(3):
                    if a[i][j] > 0 and a[i][j] == a[i][j+1] and a[i][j] == a[i][j+2]:
                        d[i][j] = 1
                        d[i][j+1] = 1
                        d[i][j+2] = 1
                        f = True
            for i in range(h):
                for j in range(5):
                    if d[i][j] == 1:
                        r += a[i][j]
                        a[i][j] = 0

            for i in range(h-2,-1,-1):
                for j in range(5):
                    if a[i][j] <= 0:
                        continue
                    k = i
                    while k < h-1 and a[k+1][j] == 0:
                        a[k+1][j] = a[k][j]
                        a[k][j] = 0
                        k += 1

        rr.append(r)

    return '\n'.join(map(str, rr))


print(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+1:
							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):
		print(ppp)
		mo(ppp,a)
	print(s)

	s = 0

	a = int(input())

Python: from itertools import groupby

def main():
    while True:
        h = int(input())
        w = 5
        if h == 0: break

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

        score = 0
        while True:
            scoreinc = 0
            for i in range(h):
                c = 0
                for key, value in groupby(field[i]):
                    newlist = list(value)
                    if len(newlist) >= 3:
                        scoreinc += newlist[0] * len(newlist)
                        newlist = [0 for i in range(len(newlist))]
                        
                    if c == 0:
                        field[i] = newlist
                    else:
                        field[i] += newlist
                    c += 1


            if scoreinc == 0: break
            score += scoreinc

            for i in range(w):
                ydata = []
                for j in range(h):
                    ydata.append(field[j][i])
                    
                ydata = sorted(ydata, key = return0)
                
                for j in range(h):
                    field[j][i] = ydata[j]

        #print(field)
        print(score)

def return0(num):
    if num == 0: return 0
    else: return 1
main()

Python: W = 5

def calc_score(ban, score):
    add_score = 0
    after_ban = []
    H = len(ban)
    for h in range(H):
        row = []
        cnt = 1
        cur = ban[h][0]
        for w in range(1, W):
            if cur == ban[h][w]:
                cnt += 1
            else:
                row.append([cur, cnt])
                cur = ban[h][w]
                cnt = 1
        row.append([cur, cnt])

        after_row = []
        for cur, cnt in row:
            if cnt >= 3:
                add_score += cur * cnt
                after_row.extend([0]*cnt)
            else:
                after_row.extend([cur]*cnt)
        after_ban.append(after_row)

    # 下が0のマスだったら落ちる
    for _ in range(10):
        for h in range(0, H-1):
            for w in range(W):
                if after_ban[h+1][w] == 0:
                    after_ban[h+1][w] = after_ban[h][w]
                    after_ban[h][w] = 0

    score += add_score
    if add_score == 0:
        return score
    else:
        return calc_score(after_ban, score)

ans = []

while True:
    H = int(input())
    if H == 0:
        break
    ban = [list(map(int, input().split())) for _ in range(H)]
    ans.append(calc_score(ban, 0))

print(*ans, sep='\n')