#include<stdio.h>

int matrix[20][5];
int numberOfNull[5];

int Execute(int);

void main()
{
	int N;
	int i, j;
	int score, tmpScore;
	
	while(1)
	{
		score = 0;
		
		scanf("%d", &N);
		
		if(N == 0) break;
		
		for(i = 10; i < 10 + N; i++)
		{
			for(j = 0; j < 5; j++)
			{
				scanf("%d", &matrix[i][j]);
			}
		}
		
		do
		{
			tmpScore = Execute(N);
			score += tmpScore;
		} while(tmpScore > 0);
		
		printf("%d\n", score);
	}
}

int Execute(int n)
{
	int i, j, k, l, count, tmp;
	int result = 0;
	
	for(i = 0; i < 5; i++)
	{
		numberOfNull[i] = 0;
	}
	
	for(i = 10 + n - 1; i > 9; i--)
	{
		for(j = 0; j < 5; j++)
		{
			tmp = numberOfNull[j];
			while(matrix[i - numberOfNull[j]][j] == 0 && (i - numberOfNull[j]) > 9)
			{
				numberOfNull[j]++;
			}
		}
		
		for(k = 0; k < 3; k++)
		{
			count = 1;
			j = k;
			while(k < 4 && matrix[i - numberOfNull[k]][k] > 0 && matrix[i - numberOfNull[k]][k] == matrix[i - numberOfNull[k + 1]][k + 1])
			{
				count++;
				k++;
			}
			if(count > 2)
			{
				result += matrix[i - numberOfNull[k]][k] * count;
				l = j;
				while(l < j + count)
				{
					matrix[i - numberOfNull[l]][l] = 0;
					l++;
				}
			}
		}
	}
	
	return result;
}