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: # https://onlinejudge.u-aizu.ac.jp/problems/1193
def delete(M):
    point = 0
    for row in M:
        for l in range(len(row)-1):
            for r in range(l + 1, len(row)):
                if row[l] != row[r]:
                    break
            if r - l >= 3:
                point += (r - l) * row[l]
                for i in range(l, r):
                    row[i] = 0
                break

    return point


def drop(M):
    for c in range(5):
        nums = []
        for i in range(len(M)-1, -1, -1):
            if M[i][c] != 0:
                nums.append(M[i][c])
            M[i][c] = 0

        for i in range(len(nums)):
            M[len(M)-1-i][c] = nums[i]


def solve(h, M):
    result = 0
    while True:
        point = delete(M)
        if point == 0:
            break
        result += point
        # print('-------')
        # for row in M:
        #     print(*row)
        # print('--drop-----')
        drop(M)
        # for row in M:
        # print(*row)
        # print('-------')
    print(result)


if __name__ == "__main__":
    while True:
        h = int(input())
        if h == 0:
            break
        M = [list(map(int, input().split())) + [0] for _ in range(h)]
        solve(h, M)

Python: #!usr/bin/env python3
import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = 10 ** 18
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

H = int(input())
while H != 0:
    table = [list(map(int, input().split())) for _ in range(H)]
    prev_ans = -1
    ans = 0
    while ans != prev_ans:
        prev_ans = ans
        for i in range(H):
            for j in range(3):
                if table[i][j] != -1:
                    if table[i][j] == table[i][j + 1] == table[i][j + 2]:
                        for l in range(j + 2, 5):
                            if table[i][j] != table[i][l]:
                                ans += (l - j) * table[i][j]
                                for k in range(j, l):
                                    table[i][k] = -1
                                break
                            elif l == 4:
                                ans += (l - j + 1) * table[i][j]
                                for k in range(j, 5):
                                    table[i][k] = -1
                                break
        for i in range(H - 2, -1, -1):
            for j in range(5):
                if table[i][j] != -1:
                    if table[i + 1][j] == -1:
                        for l in range(i + 1, H):
                            if table[l][j] != -1:
                                table[l - 1][j] = table[i][j]
                                for k in range(i, l - 1):
                                    table[k][j] = -1
                                break
                            elif l == H - 1:
                                table[l][j] = table[i][j]
                                table[i][j] = -1
                                break

    print(ans)
    H = int(input())

Python: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1193&lang=jp
import sys

while True:
    n = int(input())
    if n == 0:
        sys.exit()
    puzzle = [[] for _ in range(5)]
    # puzzle input
    for i in range(n):
        for j, stone in enumerate(list(map(int, input().split()))):
            puzzle[j].insert(0, stone)
            # puzzle[j].append(stone)
    tmp_score = 10
    ans = 0
    while tmp_score != 0:
        tmp_score = 0
        for i in range(n - 1, -1, -1):
        #for i in range(0, n):
            flg = False
            for left in range(3):
                for right in range(4, 1 + left, -1):
                        tmp_list = []
                        for idx in range(left, right+1):
                            try:
                                tmp_list.append(puzzle[idx][i])
                            except:
                                tmp_list.append(0)
                        if len(set(tmp_list)) == 1 and not (0 in tmp_list):
                            tmp_score += sum(tmp_list)
                            flg = True
                            # puzzle削除
                            for idx in range(left, right+1):
                                try:
                                    puzzle[idx].pop(i)
                                except:
                                    pass
                            break
                if flg:
                    break
        ans += tmp_score

    print(ans)