#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
  int H;
  //?????????????????¶????????\???
  while (cin >> H && H>0) {
    vector< vector<int> > c(5,vector<int>(H));
    int sum = 0;
    for (int i=H-1; i>=0; i--){
      for (int j=0; j<5; j++){
	cin >> c[j][i];
      }
    }
    //????????°??????3?????\????¨???????????????´???????????°????¨??????????0?????????
    int flg = 1; //?¶?????????¨???flg == 1
    while (flg) { 
      flg = 0;
      for (int i=H-1; i>=0; i--){
	for (int j=0; j<3; j++) {
	   int cnt = 1; //?¨???????????????°
	  if (c[j][i] != 0) {
	    int flag = 1;
	    for (int k=j+1; k<5 && flag; k++) { //????¨??????????????????????
	      if (c[j][i] == c[k][i])
		cnt++;
	      if (c[j][i] != c[k][i] || k == 4) { //?????????????????£???????????????????????????
		flag = 0;
		if (cnt >= 3) { //3?????\???????????§?????¨???
		  sum += c[j][i] * cnt; //????????????
		  for (int l=j; l<=j+cnt-1; l++) { //0???????????????
		    c[l][i] = 0;
		  }
		  flg = 1;
		}
	      }
	    }
	  }
	}
      }
      for (int j=0; j<5; j++) {
	vector<int>::iterator itr = remove(c[j].begin(), c[j].end(), 0);
	for (; itr != c[j].end(); itr++) *itr = 0;
      }
    }
    //???????????????????????????0????????£??????flg==0??¨?????????????????±???
    cout << sum << endl;
  }
}