#include <stdio.h>
#include <stdlib.h>

int main(){
    int h;
    while(scanf("%d",&h)&&h){
        int stone[h+1][6];
        int linescore = 0; //????????????????????¢
        int score = 0; //????¨?????????¢
        int i,j,k;
        for(i=1;i<=h;i++)
            for(j=0;j<5;j++)
            scanf("%d",&stone[i][j]);
            
        for(i=0;i<=h;i++)stone[i][5]=0;
        for(j=0;j<5;j++)stone[0][j]=0;
            
        const int flug=1;
        while(flug){
            //???????¶?????????????
            for(i=h;i>0;i--){
                int len=1;
                for(j=0;j<5;j++){
                    if((stone[i][j+1]!=0)&&(stone[i][j]==stone[i][j+1])){
                        len++;
                    }
                    else{
                        if(len>2){
                            linescore = (len * stone[i][j]);
                            score += linescore;
                            printf("len=%d stone[%d][%d]=%d score=%d\n",len,i,j,stone[i][j],linescore);
                            for(k=j-len+1;k<=j;k++)stone[i][k]=0;
                        }
                        len=1;
                    }
                }
            }
            //???????????§???linescore=0?????????????????\????¶????????????????????????????
            if(linescore == 0){
                printf("%d\n",score);
                break;
            }
            //???????????????
            //?¶????????????????????????????flug=0??¨??????while???????????????
            for(j=0;j<5;j++){
                int row;
                for(row=1;row<=h;row++)
                for(i=h;i>row;i--){
                    if(stone[i][j] == 0){
                        stone[i][j] = stone[i-1][j];
                        stone[i-1][j] = 0;
                    }
                }
            }//end of for(j)
            
            /*
            for(i=1;i<=h;i++){
                for(j=0;j<5;j++)printf("%d ",stone[i][j]);
                puts("");
            }
            */
        }//end of while
    }
    return 0;
}