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

#define BUF_SIZE 20

int get_int(void) {
  int num;
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return 0;
  sscanf(line, "%d", &num);
#else
#error
#endif
  return num;
}

// <arr[0]> <arr[1]> .. <arr[size-1]>
int fget_array(char *arr, int size) {
#ifdef BUF_SIZE
    char line[BUF_SIZE];
    char *tmpbuf = line;
    int i;
    if(!fgets(line, BUF_SIZE, stdin)) return -1;
    for(i = 0; i < size; i++) {
        char *tmp = strtok(tmpbuf, " ");
        arr[i] = strtol(tmp, NULL, 10);
        tmpbuf = NULL;
    }
#else
#error
#endif
    return 0;
}

#define HEIGHT_MAX 10
#define WIDTH 5
#define SET_MAX 10000
#define HEIGHS 3
#define BOARD_MAX (HEIGHT_MAX*(WIDTH+1))

void display(char *array, int width) {
    int j;
    for(j = 0; j < width; j++) {
        printf("%d ", array[j]);
    }
    putchar('\n');
    return;
}

int judge(char *arr, int size) {
    char cur = arr[0];
    int cnt = 1;
    int i, j;
    int score = 0;
    for(i = 1; i < size; i++) {
        char next = arr[i];
        if(cur == next) {
            cnt++; continue;
        }
        if(cnt >= HEIGHS) {
            score += cnt*cur;
            // disappeared until previous ball
            for(j = i-cnt; j <= i-1; j++) {
                // when disappearred, labeled as 0
                arr[j] = 0;
            }
        }
        cur = next; cnt = 1;
    }
#ifdef DEBUG
    printf("score: %d\n", score);
#endif
    return score;
}

int main(void) {
    int i;
    int scores[SET_MAX];
    int idx = 0;
    while(1) {
        int height = get_int();
        if(!height) break;
        // array[*][WIDTH]: guard(0)
        char array[HEIGHT_MAX][WIDTH+1] = {0};

        // 0: highest
        for(i = height-1; i >= 0; i--) {
            fget_array(array[i], WIDTH);
        }
        int score = 0;
        while(1) {
            int w, h;
            char newbuf[HEIGHT_MAX][WIDTH+1] = {0};
#ifdef DEBUG
            // display currrent board
            for(i = height-1; i >= 0; i--) {
                display(array[i], WIDTH+1);
            }
#endif
            // judge
            int new_score = 0;
            for(h = 0; h < height; h++) {
                new_score += judge(array[h], WIDTH+1);
            }
            if(!new_score) break;
            score += new_score;
            // drop
            for(w = 0; w < WIDTH; w++) {
                int idx = 0;
                for(h = 0; h < height; h++) {
                    char cur = array[h][w];
                    if(!cur) continue;
                    newbuf[idx++][w] = cur;
                }
            }
            // replace array with new
            memcpy(array[0], newbuf[0], BOARD_MAX);
        }
        scores[idx++] = score;
    }
    for(i = 0; i < idx; i++) {
        printf("%d\n", scores[i]);
    }
    return 0;
}
