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++: 
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <tuple>
#include <set>
#include <stack>
#include <queue>


using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using pci = pair<char, int>;
using vpci = vector<pci>;
#define out(S) cout<<(S)<<endl;
#define REP(i,b) for(size_t i=0;i<(b);i++)
#define rREP(i,b) for(int i=(b)-1;i>=0;i--)
#define FOR(i,a,b) for(size_t i=(a);i<(b);i++)
#define rFOR(i,a,b) for(int i=(a)-1;i>=(b);i--)
#define Foreach(item,collection) for(auto item:collection)
#define mod(i) ((i) % (ll)(1e9 + 7))
ll modpow(ll i, ll j) { ll tmp = 1; while (j) { if (j % 2)tmp = mod(tmp*i); i = mod(i*i); j /= 2; }return tmp; }
#define divmod(a,b) (mod(a * modpow((ll)b,(ll)(1e9 + 5))))
#define Yes out("Yes")
#define No out("No")
#define NO out("NO")
#define YES out("YES")
#define INF 1e10;
#define ShowAll(collection) for(auto i:collection){out(i);}
#define IfOut(condition,text) if((condition)){out(text);return 0;}
#define IfeOut(condition,itext,etext) if(condition){out(itext);return 0;}else{out(etext);return 0;}
#define Select(collection,condition,result) Foreach(i,collection){result+=condition;}
#define GetAuto(_N,vect) {cin>>_N;vect.resize(_N);REP(i,_N){cin>>vect[i];}}
#define beginend(vec) (vec).begin(),(vec).end()
#define pb(item) push_back(item)
#define removeAll(vec,deleg) auto itr=remove_if(beginend(vec),deleg);vec.erase(itr,vec.end());
void removeAt(string& s, int index) { s.erase(index, 1); }
template<typename T> void removeAt(vector<T>& v, int index) { v.erase(v.begin() + index); }
ll manhattanDistance(ll x1, ll y1, ll x2, ll y2) { return (abs(x2 - x1) + abs(y2 - y1)); }
vector<ll> sieveOfEratosthenes(int max) {ll prime;double sqrtmax = sqrt(max);vector<ll> primeVec, searchVec;FOR(i, 2, max + 1) searchVec.push_back(i);do{prime = searchVec[0];primeVec.push_back(prime);auto itr = remove_if(beginend(searchVec), [=](ll x)->bool {return x%prime == 0; });searchVec.erase(itr, searchVec.end());} while (prime < sqrtmax);primeVec.reserve(primeVec.size() + searchVec.size());primeVec.insert(primeVec.end(), beginend(searchVec));return primeVec;}
bool isPrime(ll x) {if (x == 2) return true;if (x < 2 || x % 2 == 0) return false;ll i = 3;while (i <= sqrt(x)) {if (x%i == 0) return false;i += 2;}return true;}

int n;
vvi pazle;

int pzdelete(int y) {
	int x = -1;
	int cnt = 0;
	int add = 0;
	REP(i, 5) {
		if (x == -1) x = pazle[y][i];

		if (x == pazle[y][i])cnt++;
		else { x = pazle[y][i]; cnt = 1; }

		if (cnt >= 3) {
			REP(k, cnt) {
				pazle[y][i - k] = 0;
				add = x*cnt;
			}
		}
	}
	return add;
}

void pzdown() {
	REP(i, 5) {
		rREP(k, n) {
			if (pazle[k][i]) continue;
			//0?????£???
			int g = k-1;
			while (g >= 0) { if (pazle[g][i] != 0) { break; } g--; }
			if (g < 0) continue;
			pazle[k][i] = pazle[g][i];
			pazle[g][i] = 0;
		}
	}
}

void pzshow() {
	Foreach(i, pazle) cout << i[0] << " " << i[1] << " " << i[2] << " " << i[3] << " " << i[4] << endl;
}

void solve() {
	ll ans = 0;
	while (1)
	{
		int add = 0;
		REP(i, n) {
			add += pzdelete(i);
		}
		if (add == 0) break;
		ans += add;
		::pzdown();
	}
	out(ans);
	return;
}
int main() {
	while (cin>>n,n)
	{
		pazle.clear();
		pazle.resize(n);
		REP(i, n) {
			vi x(5);
			REP(k, 5)cin >> x[k];
			pazle[i] = x;
		}
		::solve();
	}

	return 0;
}

C++: #include <iostream>
using namespace std;

int map[10][5], h, ans = 0;

int main() {

	while (true) {
		cin >> h;
		if (h == 0)break;

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < 5; j++) {
				cin >> map[i][j];
			}
		}

		while (true) {
			int ans2 = 0;
			bool flag = false;
			for (int i = 0; i < h; i++) {
				int c = 0;
				//cout << "i:" << i << endl;
				for (int j = 1; j < 5; j++) {
					if (map[i][j - 1] == map[i][j]) {
						c++;
						//cout << "map[h][j - 1]:" << map[h][j - 1] << " map[h][j]:" << map[h][j] << endl;
					}
					else if (c >= 2) {
						//flag = true;
						for (int k = 0; k < c + 1; k++) {
							ans2 += map[i][j - 1 - k];
							map[i][j - 1 - k] = 0;
						}
						c = 0;
					}//??£????????£??????????????????????????????
					else {
						c = 0;
					}

					if (map[i][j - 1] == map[i][j] && c >= 2 && j == 4) {
						//flag = true;
						for (int k = 0; k < c + 1; k++) {
							ans2 += map[i][j - k];
							map[i][j - k] = 0;
						}
					}
					//cout << "c;" << c << " ";
				}
				//cout << endl;
				c = 0;
			}

			/*for (int i = 0; i < h; i++) {
				for (int j = 0; j < 5; j++) {
					cout << map[i][j] << " ";
				}
				cout << endl;
			}
			cout << endl;*/

			for (int i = h - 1; i >= 0; i--) {
				for (int j = 0; j < 5; j++) {
					if (map[i][j] == 0) {
						for (int k = i; k >= 0; k--) {
							if (map[k][j] != 0) {
								flag = true;
								map[i][j] = map[k][j];
								map[k][j] = 0;
								break;
							}
						}
					}
				}
			}
			/*for (int i = 0; i < h; i++) {
				for (int j = 0; j < 5; j++) {
					cout << map[i][j] << " ";
				}
				cout << endl;
			}*/
			
			//cout << "ans2:" << ans2 << endl;
			ans += ans2;
			if (!flag)break;
		}

		cout << ans << endl;
		ans = 0;
	}
}