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 <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1 ; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1 ; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e9+100;
const bool debug = 0;
/***************************************/

int H, W = 5;
int maps[10][5];

int solera() {
	int res = 0;
	REP(i, H) {
		int eq[5] = {};
		REP(j, W) {
			if (!maps[i][j]) eq[j] = 0;
			else if (j && maps[i][j] == maps[i][j - 1]) eq[j] = eq[j - 1] + 1;
			else eq[j] = 1;
		}
		for (int j = W - 2; j >= 0; j--) {
			if (maps[i][j] == maps[i][j + 1]) eq[j] = eq[j + 1];
		}
		REP(j, W) if (eq[j] >= 3) {
			res += maps[i][j];
			maps[i][j] = 0;
		}
	}
	return res;
}

bool soldro() {
	bool res = false;
	
	REP(j, W) {
		for (int i = H - 2; i >= 0; i--) {
			if (maps[i + 1][j] == 0 && maps[i][j]) {
				maps[i + 1][j] = maps[i][j];
				maps[i][j] = 0;
				res = true;
			}
		}
	}
	return res;
}

int main() {
	while (cin >> H, H) {
		REP(i, H) REP(j, W) scanf("%d", &maps[i][j]);
		
		int ans = 0;
		while (true) {
			int cur = solera();
			ans += cur;
			if (cur == 0) break;
			while (soldro());
		}
		
		if (debug) printf("@ ");
		cout << ans << endl;
	}
	return 0;
}

C++: #include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(aa, (v).size()) { cout << v[a]; if (a != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
const int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
bool fall(vvi &v) {
	int h = v.size();
	bool updated = false;
	REP(j, 5) {
		int flag = true;
		while (flag) {
			flag = false;
			RREP(i, h - 1) {
				if (v[i + 1][j] == 0 && v[i][j] != 0) {
					v[i + 1][j] = v[i][j];
					v[i][j] = 0;
					flag = true;
					updated = true;
				}
			}
		}
	}
	return updated;
}
int erase(vvi &v) {
	int h = v.size();
	int ret = 0;
	vvi b(h, vi(5, 0));
	REP(i, h) {
		REP(j, 3) {
			int c = v[i][j], flag = 1;
			REP(k, 3) {
				if (v[i][j + k] != c)flag = 0;
			}
			if (flag) {
				REP(k, 3) {
					b[i][j + k] = 1;
				}
			}
		}
	}
	REP(i, h) {
		REP(j, 5) {
			if (b[i][j]) {
				ret += v[i][j];
				v[i][j] = 0;

			}
		}
	}
	return ret;
}
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (1) {
		int h;
		cin >> h;
		if (!h)break;
		vvi v(h, vi(5));
		REP(i, h) {
			REP(j, 5) {
				cin >> v[i][j];
			}
		}
		int ans = 0;
		do {
			ans += erase(v);
		} while (fall(v));
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define rp(a) while(a--)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
int main(){
	int n;
	while(cin>>n,n){
		vector<vector<char> >in(5,vector<char>(n));
		rep(i,n)rep(j,5){
			int q;
			cin>>q;
			in[j][n-1-i]=q+'0';
		}
		//rep(i,5)rep(j,n)cout<<in[i][j]<<" ";
		int out=0;
		while(1){
			rep(i,n){
				char tmp=in[2][i];
				if(tmp=='#')continue;
				string s="";
				rep(j,5)s+=in[j][i];
				string tmps="";
				rep(j,3)tmps+=tmp;
				if(s.find(tmps)!=string::npos){
					rep(j,5){
						if(((j==0&&in[1][i]==tmp)||j==1||j==2||j==3||(j==4&&in[3][i]=='!'))&&in[j][i]==tmp)
							in[j][i]='!';
					}
					rep(j,5)if(in[j][i]=='!')out+=tmp-'0';
				}
			}
			bool h=false;
			rep(i,5)for(int j=0;j<n;j++)if(in[4-i][j]=='!'){
				in[4-i].erase(in[4-i].begin()+j);
				in[4-i].pb('#');
				j--;
				h=true;
			}
			if(!h)break;
		}
		cout<<out<<endl;
	}
}