Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		h := io.NextInt()
		if h == 0 {
			break
		}
		p := make([][]int, h)
		for i := 0; i < h; i++ {
			p[i] = make([]int, 5)
			for j := 0; j < 5; j++ {
				p[i][j] = io.NextInt()
			}
		}
		ans := 0
		for {
			sc := puyo(p, h)
			if sc == 0 {
				break
			}
			fall(p, h)
			ans += sc
		}
		io.Println(ans)
	}
}

func puyo(p [][]int, h int) int {
	sc := 0
	for i := 0; i < h; i++ {
		for j := 0; j < 3; j++ {
			var cnt int
			for cnt = 1; j+cnt < 5; cnt++ {
				if p[i][j] != p[i][j+cnt] {
					break
				}
			}
			if cnt > 2 {
				sc += p[i][j]*cnt
				for k := 0; k < cnt; k++ {
					p[i][j+k] = 0
				}
			}
		}
	}
	return sc
}

func fall(p [][]int, h int) {
	for j := 0; j < 5; j++ {
		for k := 1; k < h; k++ {
			for i := h - 1; i >= k; i-- {
				if p[i][j] == 0 {
					p[i][j] = p[i-1][j]
					p[i-1][j] = 0
				}
			}
		}
	}
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: //Chain Disappearance Puzzle
#include<bits/stdc++.h>
using namespace std;

int n;
int b[10][5];

void print(){
  for(int i=0; i<n; i++){
    for(int j=0; j<5; j++)
      cout<<b[i][j]<<' ';
    cout<<endl;
  }
  cout<<endl;
}

int solve(){
  int ret=0;
  //print();
  for(int i=0; i<n; i++){
    for(int j=0; j<3; j++){
      int p=1;
      while(j+p<5 && b[i][j]!=0 && b[i][j]==b[i][j+p])p++;
      if(p>=3){
        ret+=b[i][j]*p;
        while(p--)b[i][j+p]=0;
      }
    }
  }

  for(int i=n-1; i>=0; i--){
    for(int j=0; j<5; j++){
      if(b[i][j]!=0){
        int p=1;
        while(i+p<n && b[i+p][j]==0){
          swap(b[i+p-1][j], b[i+p][j]);
          p++;
        }
      }
    }
  }

  if(ret==0)return ret;
  else return ret+solve();
}

int main(){
  while(true){
    cin>>n;
    if(n==0)break;
    memset(b, 0, sizeof(b));
    for(int i=0; i<n; i++)
      for(int j=0; j<5; j++)
        cin>>b[i][j];
    cout<<solve()<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second

ll h;
vector<ll> v[5];

int main(){
	while (cin >> h&&h){
		ll ans = 0;
		REP(i, 5){
			v[i].clear();
			v[i].resize(h);
		}
		REP(i, h){
			REP(j, 5){
				cin >> v[j][h - 1 - i];
			}
		}
		bool f = 1;
		while (f){
			//REP(i, 5){
			//	REP(j, v[i].size()){
			//		cout << v[i][j] << "  ";
			//	}
			//	cout << endl;
			//}
			f = 0;
			vector<pair<P, ll>> vpi;
			REP(i, h){
				ll cnt = 1, pos = 0;
				REP(j, 5 - 1){
					if (v[j][i] == v[j + 1][i]){
						if (v[j][i] != -1)cnt++;
						else cnt = 0;
					}
					else{
						if (cnt >= 3){
							vpi.push_back({ P(pos, cnt), i });
						}
						pos = j + 1;
						cnt = 1;
					}
				}
				if (cnt >= 3){
					vpi.push_back({ P(pos, cnt), i });
				}
			}
			REP(i, vpi.size()){
				f = 1;
				ll he = vpi[i].second, pos = vpi[i].first.first, cnt = vpi[i].first.second;
				//cout << he << " " << pos << " " << cnt << endl;
				ans += (v[pos][he] * cnt);
				REP(j, cnt)v[pos + j][he] = -1;
			}
			REP(i, 5){
				auto it = v[i].begin();
				while (it != v[i].end()){
					if (*it == -1){
						it = v[i].erase(it);
					}
					else it++;
				}
			}
			REP(i, 5){
				while (v[i].size() < h){
					v[i].push_back(-1);
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
int W = 5;
int H;
int field[15][6];

int f() {
    int now = 0;
    for(int h = 1; h <= H; h++) {
        //cerr << "h: " << h << endl;
        for(int delta = 5; delta >= 3; delta--) {
            //cerr << "delta : " << delta << endl;
            for(int w = 1; delta + w <= W + 1; w++) {
                //cerr << "w: " << w << endl;
                bool ok = true;
                for(int noww = w + 1; noww <= w + delta - 1; noww++) {
                    if(field[h][noww] != field[h][w]) ok = false;
                }
                if(ok) {
                    now += delta * field[h][w];
                    for(int noww = w; noww <= w + delta - 1; noww++) {
                        field[h][noww] = 0;
                    }
                }
            }
        }
    }
    return now;
}

void drop() {
    /*
    cerr << "before" << endl;
    for(int h = H; h >= 1; h--) {
        for(int w = 1; w <= W; w++) cerr << field[h][w] << " ";
        cerr << endl;
    }
    */
    for(int _ = 1; _ <= 15 * 15; _++) {
    for(int h = H; h >= 2; h--) {
        for(int w = 1; w <= W; w++) {
            if(field[h-1][w] == 0) {
                field[h-1][w] = field[h][w];
                field[h][w] = 0;
            }
        }
    }
    }
    /*
    cerr << "after" << endl;
    for(int h = H; h >= 1; h--) {
        for(int w = 1; w <= W; w++) cerr << field[h][w] << " ";
        cerr << endl;
    }
    */
}

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true) {
        cin >> H;
        if(H == 0) break;
        for(int h = H; h >= 1; h--) {
            for(int w = 1; w <= W; w++) cin >> field[h][w];
        }
        int score = 0;
        while(true) {
            int now = f();
            //cerr << score << " "<< now << endl;
            if(now == 0) break;
            score += now;
            drop();
        }
        cout << score << endl;
    }
    return 0;
}