human
Chain Disappearance Puzzle

We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy.



The puzzle proceeds taking the following steps.

1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously.
2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1.


The score of this puzzle is the sum of the digits on the disappeared stones.

Write a program that calculates the score of given configurations of stones.



Input

The input consists of multiple datasets. Each dataset is formed as follows.

> Board height H
>  Stone placement of the row 1
>  Stone placement of the row 2
>  ...
>  Stone placement of the row H
>

The first line specifies the height ( H  ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order.

The input ends with a line with a single zero.

Output

For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores.

Sample Input

1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6...
Solve the task in PYTHON3.
 Can you provide a the Python program you requested?
assistant
#Here is the Python program you requested.

#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def check(y,x):
        res = 0
        c = s[x][y]
        for x in range(x,5):
            if s[x][y] != c:
                break
            res += 1
        if res < 3:
            return 0
        else:
            return res

    def update(y,x,d):
        for i in range(x,x+d):
            s[i][y] = 0

    def fall(s):
        q = [[] for i in range(5)]
        for x in range(5):
            for y in range(h):
                if s[x][y]:
                    q[x].append(s[x][y])
            for y in range(h-len(q[x])):
                q[x].append(0)
        return q

    while 1:
        h = I()
        if h == 0:
            break
        s = LIR(h)
        s = [[s[-1-y][x] for y in range(h)] for x in range(5)]
        ans = 0
        f = 1
        while f:
            f = 0
            for y in range(h):
                for x in range(3):
                    if s[x][y]:
                        d = check(y,x)
                        ans += s[x][y]*d
                        if d:
                            update(y,x,d)
                            f = 1
            if f:
                s = fall(s)
        print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()