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: # -*- coding: utf-8 -*-
from collections import Counter
def inpl(): return list(map(int, input().split()))

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

Python: def scan_row(row):
    if row[0] == row[1] == row[2]:
        if row[2] == row[3]:
            if row[3] == row[4]:
                return (row[0]*5, 0, 5)
            else:
                return (row[0]*4, 0 ,4)
        else:
            return (row[0]*3, 0 ,3)
    elif row[1] == row[2] == row[3]:
        if row[3] == row[4]:
            return (row[1]*4, 1, 4)
        else:
            return (row[1]*3, 1, 3)
    elif row[2] == row[3] == row[4]:
        return (row[2]*3 , 2, 3)
    return (-1,-1,-1)

while True:
    H = int(input())
    if H == 0: break
    src = [list(map(int,input().split())) for i in range(H)]
    mem = [[] for i in range(5)]
    for row in reversed(src):
        for x in range(5):
            mem[x].append(row[x])
    ans = 0
    while True:
        erase = []
        for h in range(max(map(len,mem))):
            row = tuple(map(lambda x:x[h] if h < len(x) else -1, mem))
            score,start,l = scan_row(row)
            if score <= 0: continue
            ans += score
            erase.append((h,start,l))
        if not erase: break
        for h,start,l in reversed(erase):
            for i in range(start,start+l):
                mem[i].pop(h)
    print(ans)

Python: 
while True:
    h = int(input())
    if h==0:
        break
    puyo = [list(map(int, input().split())) for _ in range(h)]

    p = 0
    fin = 0
    turn = 1
    while fin==0:
        #[print(*puyo[i]) for i in range(h)]
        fin = 1
        for i in range(h):
            cnt = 1
            for j in range(1, 5):
                if puyo[i][j-1] == puyo[i][j] and puyo[i][j]>0:
                    cnt += 1
                else:
                    if cnt>=3:
                        p += cnt * puyo[i][j-1]
                        for k in range(cnt):
                            puyo[i][j-k-1] = -1
                            fin = 0
                    cnt = 1
                if j==4:
                    if cnt>=3:
                        p += cnt * puyo[i][j-1]
                        for k in range(cnt):
                            puyo[i][j-k] = -1
                            fin = 0
        #print('after remove')
        #[print(*puyo[i]) for i in range(h)]              
        for i in range(5):
            d_fin = 0
            while d_fin==0:
                d_fin = 1
                for j in range(h-1, 0, -1):
                    if puyo[j][i]==-1 and puyo[j-1][i]>0:
                        puyo[j][i] = puyo[j-1][i]
                        puyo[j-1][i] = -1
                        d_fin = 0
        #print(f'turn {turn}')
        turn += 1
    print(p)