#include <stdio.h>

int main(void) {
	int m[16][8],i,j,l,p,s,r;
	while(scanf("%d",&l) , l != 0){
		s = 0;
		for(i = 0;i <= 15;i++){
			for(j = 0;j <= 7;j++){
				m[i][j] = 0;
			}
		}
		for(i = l;i >= 1;i--){
			scanf("%d%d%d%d%d",&m[i][1],&m[i][2],&m[i][3],&m[i][4],&m[i][5]);
		}
		p = 1;
		while(p != 0){
			p = 0;
			for(i = 1;i <= l;i++){
				if(m[i][1] != 0 && m[i][1] == m[i][2] && m[i][2] == m[i][3] && m[i][3] == m[i][4] && m[i][4] == m[i][5]){
					p = 1;
					s+=((m[i][1]) * 5);
					m[i][1] = 0;m[i][2] = 0;m[i][3] = 0;m[i][4] = 0;m[i][5] = 0;
				}
				else if(m[i][1] != 0 && m[i][1] == m[i][2] && m[i][2] == m[i][3] && m[i][3] == m[i][4]){
					p = 1;
					s+=((m[i][1]) * 4);
					m[i][1] = 0;m[i][2] = 0;m[i][3] = 0;m[i][4] = 0;
				}
				else if(m[i][2] != 0 && m[i][2] == m[i][3] && m[i][3] == m[i][4] && m[i][4] == m[i][5]){
					p = 1;
					s+=((m[i][2]) * 4);
					m[i][2] = 0;m[i][3] = 0;m[i][4] = 0;m[i][5] = 0;
				}
				else if(m[i][1] != 0 && m[i][1] == m[i][2] && m[i][2] == m[i][3]){
					p = 1;
					s+=((m[i][1]) * 3);
					m[i][1] = 0;m[i][2] = 0;m[i][3] = 0;
				}
				else if(m[i][2] != 0 && m[i][2] == m[i][3] && m[i][3] == m[i][4]){
					p = 1;
					s+=((m[i][2]) * 3);
					m[i][2] = 0;m[i][3] = 0;m[i][4] = 0;
				}
				else if(m[i][3] != 0 && m[i][3] == m[i][4] && m[i][4] == m[i][5]){
					p = 1;
					s+=((m[i][3]) * 3);
					m[i][3] = 0;m[i][4] = 0;m[i][5] = 0;
				}
			}
			
			for(j = 1;j <= 5;j++){
				r = 1;
				for(i = 1;i <= 10;i++){
					if(m[i][j] != 0){
						m[r][j] = m[i][j];
						r++;
					}
				}
				for(i = r;i <= 10;i++){
					m[i][j] = 0;
				}
			}
		}
		printf("%d\n",s);
	}
	return 0;
}