#include<stdio.h>

int main()
{
  short H,i,j,k;
  int puzzle[10][5];
  int total,score,total2;

  while(1){
    total = 0;
    score = 0;
    total2 = 0;

    scanf("%hd",&H);

    if(H == 0){
      break;
    }

    for(i = 0;i < H;i++){
      for(j = 0;j < 5;j++){
        scanf("%d",&puzzle[i][j]);
      }
    }

    while(1){
      for(i = 0;i < H;i++){
        for(j = 0;j < 3;j++){
          if(puzzle[i][j] == puzzle[i][j+1] && puzzle[i][j] == puzzle[i][j+2]){
            if(j == 0){
              if(puzzle[i][j] == puzzle[i][j+3]){
                if(puzzle[i][j] == puzzle[i][j+4]){
                  score = puzzle[i][j] * 5;
                  for(k = 0;k < 5;k++){
                    puzzle[i][k] = 0;
                  }
                  break;
                }
                score =puzzle[i][j] * 4;
                for(k = 0;k < 4;k++){
                  puzzle[i][k] = 0;
                }
                break;
              }
              score =puzzle[i][j] * 3;
              for(k = 0;k < 3;k++){
                puzzle[i][k] = 0;
              }
              break;
            }
            else if(j == 1){
              if(puzzle[i][j] == puzzle[i][j+3]){
                score =puzzle[i][j] * 4;
                for(k = 1;k < 5;k++){
                  puzzle[i][k] = 0;
                }
                break;
              }
              score =puzzle[i][j] * 3;
              for(k = 1;k < 4;k++){
                puzzle[i][k] = 0;
              }
              break;
            }
            else{
              score =puzzle[i][j] * 3;
              for(k = 2;k < 5;k++){
                puzzle[i][k] = 0;
              }
              break;
            }
          }
        }
        total = total + score;
        score = 0;
      }

      if(total2 == total){
        break;
      }
      else{
        total2 = total;
      }

      for(k = 1;k < 5;k++){
        for(i = 0;i < H-1;i++){
          for(j = 0;j < 5;j++){
            if(puzzle[i+1][j] == 0){
              puzzle[i+1][j] = puzzle[i][j];
              puzzle[i][j] = 0;
            }
          }
        }
      }
    }
    printf("%d\n",total);
  }

  return 0;
}