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.


<image>


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.


<image>


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 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0


Output for the Sample Input


36
38
99
0
72






Example

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 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0


Output

36
38
99
0
72

import java.util.*;
import java.io.*;
import java.math.BigDecimal;
import java.awt.geom.*;
import static java.util.Arrays.*;
import static java.lang.Math.*;

public class Main{

    static final Reader sc = new Reader();
    static final PrintWriter out = new PrintWriter(System.out,false);

    public static void main(String[] args) throws Exception {
        while(true){
        	int h = sc.nextInt();
        	if(h==0){
        		break;
        	}
        	ArrayList<Integer> list1 = new ArrayList<Integer>();
        	ArrayList<Integer> list2 = new ArrayList<Integer>();
        	ArrayList<Integer> list3 = new ArrayList<Integer>();
        	ArrayList<Integer> list4 = new ArrayList<Integer>();
        	ArrayList<Integer> list5 = new ArrayList<Integer>();
        	for(int i=0;i<=h;i++){
        		list1.add(-1);
        		list2.add(-2);
        		list3.add(-3);
        		list4.add(-4);
        		list5.add(-5);
        	}
        	for(int i=0;i<h;i++){
        		list1.set(h-i-1,sc.nextInt());
        		list2.set(h-i-1,sc.nextInt());
        		list3.set(h-i-1,sc.nextInt());
        		list4.set(h-i-1,sc.nextInt());
        		list5.set(h-i-1,sc.nextInt());
        	}
        	int ans = 0;
        	while(true){
        		boolean judge = false;
        		for(int i=h-1;i>=0;i--){
        			int a;
        			int b;
        			int c;
        			int d;
        			int e;
        			if(i<list1.size()){
        				a = list1.get(i);
        			}
        			else{
        				a = -1;
        			}
        			if(i<list2.size()){
        				b = list2.get(i);
        			}
        			else{
        				b = -2;
        			}
        			if(i<list3.size()){
        				c = list3.get(i);
        			}
        			else{
        				c = -3;
        			}
        			if(i<list4.size()){
        				d = list4.get(i);
        			}
        			else{
        				d = -4;
        			}
        			if(i<list5.size()){
        				e = list5.get(i);
        			}
        			else{
        				e = -5;
        			}
        			if(a==b && b==c){
        				if(c==d && d==e){
        					ans += 2*d;
        					list4.remove(i);
        					list5.remove(i);
        				}
        				else if(c==d){
        					ans += d;
        					list4.remove(i);
        				}
        				ans += 3*a;
        				list1.remove(i);
        				list2.remove(i);
        				list3.remove(i);
        				judge = true;
        			}
        			else if(b==c && c==d){
        				if(d==e){
        					ans += e;
        					list5.remove(i);
        				}
        				ans += 3*b;
        				list2.remove(i);
        				list3.remove(i);
        				list4.remove(i);
        				judge = true;
        			}
        			else if(c==d && d==e){
        				ans += 3*c;
        				list3.remove(i);
        				list4.remove(i);
        				list5.remove(i);
        				judge = true;
        			}
        		}
        		if(!judge){
        			break;
        		}
        	}
        	out.println(ans);
        	out.flush();
        }
        sc.close();
        out.close();
    }

    static void trace(Object... o) { System.out.println(Arrays.deepToString(o));}
}

class Reader {
    private final InputStream sc;
    private final byte[] buf = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    public Reader() { this(System.in);}
    public Reader(InputStream source) { this.sc = source;}

    private boolean hasNextByte() {
        if (ptr < buflen) return true;
        ptr = 0;
        try{
            buflen = sc

.read(buf);
        }catch (IOException e) {e.printStackTrace();}
        if (buflen <= 0) return false;
        return true;
    }

    private int readByte() { if (hasNextByte()) return buf[ptr++]; else return -1;}

    private boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}

    private void skip() { while(hasNextByte() && !isPrintableChar(buf[ptr])) ptr++;}

    public boolean hasNext() {skip(); return hasNextByte();}

    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        boolean minus = false;
        long num = readByte();

        if(num == '-'){
            num = 0;
            minus = true;
        }else if (num < '0' || '9' < num){
            throw new NumberFormatException();
        }else{
            num -= '0';
        }
        
        while(true){
            int b = readByte();
            if('0' <= b && b <= '9')
                num = num * 10 + (b - '0');
            else if(b == -1 || !isPrintableChar(b))
                return minus ? -num : num;
            else
                throw new NoSuchElementException();
        }
    }

    public int nextInt() {
        long num = nextLong();
        if (num < Integer.MIN_VALUE || Integer.MAX_VALUE < num)
            throw new NumberFormatException();
        return (int)num;
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }

    public char nextChar() {
        if (!hasNext()) throw new NoSuchElementException();
        return (char)readByte();
    }

    public String nextLine() {
        while (hasNextByte() && (buf[ptr] == '\n' || buf[ptr] == '\r')) ptr++;
        if (!hasNextByte()) throw new NoSuchElementException();

        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (b != '\n' && b != '\r') {
            sb.appendCodePoint(b);
            b = readByte();
        }

        return sb.toString();
    }

    public int[] nextIntArray(int n) {
        int[] res = new int[n];
        for (int i=0; i<n; i++) res[i] = nextInt();
        return res;
    }

    public char[] nextCharArray(int n) {
        char[] res = new char[n];
        for (int i=0; i<n; i++) res[i] = nextChar();
        return res;
    }

    public void close() {try{ sc.close();}catch(IOException e){ e.printStackTrace();}};
}