#include <bits/stdc++.h>

using namespace std;

int h, ans;
int a[10][5];

bool change(void) {
  bool res = true;
  for (int i = 0; i < h; i++) {
    int l = 0, r = 0;
    while (l < 5) {
      while (r + 1 < 5 && a[i][r] > 0 && a[i][r] == a[i][r + 1]) r++;
      if (r - l + 1 >= 3) {
        ans += a[i][l] * (r - l + 1);
        for (int j = l; j <= r; j++) {
          a[i][j] = -1;
        }
        res &= false;
      }
      l = ++r;
    }
  }
  return res;
}

int main(void) {
  while (cin >> h, h) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < 5; j++) {
        cin >> a[i][j];
      }
    }

    ans = 0;
    while (true) {
      if (change()) break;
      for (int i = 0; i < 5; i++) {
        for (int j = h - 1; j >= 0; j--) {
          if (a[j][i] > 0) continue;
          int s = j;
          while (s >= 0 && a[s][i] < 0) s--;
          if (s < 0) break;
          while (++s <= j) swap(a[s - 1][i], a[s][i]);
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}
