#include <stdio.h>

int main(void)
{

  int H,i,j,k,K,n,score,l;
  
  while(1){
    score=n=0;
    scanf("%d", &H);
    int d[H][5];
    for(i = 0;i < H;i++){
      for(j = 0;j < 5;j++){
	d[i][j]=0;
      }
    }
    for(i = 0;i < H;i++){
      scanf("%d %d %d %d %d",&d[i][0],&d[i][1],&d[i][2],&d[i][3],&d[i][4]);
    }
    while(n<H){
      for(i = 0;i < H;i++){
	for(j = 0;j < 3;j++){
	  /*五個消し*/
	  if(d[i][j] == d[i][j+1] && d[i][j+1] == d[i][j+2] && d[i][j+2] == d[i][j+3] && d[i][j+3] == d[i][j+4] && d[i][j] != 0){
	    score = score + d[i][j] * 5;
	    d[i][j] = d[i][j+1] = d[i][j+2] = d[i][j+3] = d[i][j+4] = 0;
	    /*チェック用*/
	    /* printf("%d\n", score);*/
	  }
	  /*四個消し*/
	  else if(d[i][j] == d[i][j+1] && d[i][j+1] == d[i][j+2] && d[i][j+2] == d[i][j+3] && d[i][j] != 0){
	    score = score + d[i][j] * 4;
	    d[i][j] = d[i][j+1] = d[i][j+2] = d[i][j+3] = 0;
	    /*チェック用*/
	   /* printf("%d\n", score);*/
	  }
	  /*三個消し*/
	  else if(d[i][j] == d[i][j+1] && d[i][j+1] == d[i][j+2] && d[i][j] != 0){
	    score = score + d[i][j] * 3;
	    d[i][j] = d[i][j+1] = d[i][j+2] = 0;
	    /*チェック用*/
	    /*printf("%d\n", score); */ 
	  }
	}
      }
      for(K = 1;K < H;K++){
	for(i = 0;i < H;i++){
	  for(j = 0;j < 5;j++){
	    if(d[i][j] == 0){
	      for(k = 0;k <= i;k++){
		d[i-k][j]=d[i-k-1][j];
	      }
	    }
	  }
	}
      }
      n=n+1;
    }
    printf("%d\n", score);
  }
  return 0;
}