human
Write me a the Python code you requested given the following:
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...
assistant
#Here is the Python code you requested.

from collections import deque

ans_list = []

def solve():
    H = int(input())
    if H == 0:
        return "end"
    grid = [list(map(int,input().split())) for _ in range(H)]
    
    def erase(i):
        res = 0
        a = grid[i]
        dq = deque()
        for j in range(5):
            if not dq:
                dq.append(a[j])
            elif dq[-1] == a[j] and a[j] != 0:
                dq.append(a[j])
            else:
                if len(dq) >= 3:
                    res += dq[-1] * len(dq)
                    # 消したところを0に書き換える
                    for nj in range(j-len(dq),j):
                        grid[i][nj] = 0
                dq.clear()
                dq.append(a[j])
        if len(dq) >= 3:
            res += dq[-1] * len(dq)
            # 消したところを0に書き換える
            for nj in range(j+1-len(dq),j+1):
                grid[i][nj] = 0
        return res
    
    def drop(j):
        b = [grid[i][j] for i in range(H)]
        dq = deque()
        for i in range(H)[::-1]:
            # 下から詰めていく 左から取り出して下から詰める
            if b[i] != 0:
                dq.append(b[i])
        for ni in range(H)[::-1]:
            if dq:
                num = dq.popleft()
            else:
                num = 0
            grid[ni][j] = num

    score = 0

    while True:
        res = 0
        for i in range(H):
            res += erase(i)
        score += res
        if res == 0:
            break
        for j in range(5):
            drop(j)
    
    return score

while True:
    ans = solve()
    if ans == "end":
        break
    ans_list.append(ans)

for ans in ans_list:
    print(ans)