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 <algorithm>
#include <vector>
using namespace std;

const int MAX_H = 100;

int field[5][MAX_H];
int H;

int erase(int h) {
	int cnt = 1,max_cnt = 0,n = -1;
	for(int i = 1;i < 5;i++) {
		if(field[i][h] == field[i - 1][h]) cnt++;
		else cnt = 1;
		if(cnt > max_cnt) {
			max_cnt = cnt;
			n = field[i][h];
		}
	}
	if(max_cnt >= 3) {
		for(int i = 0;i < 5;i++) {
			if(field[i][h] == n) field[i][h] = 0;
		}
		return max_cnt * n;
	} else return 0;
}

int main() {
	
	while(cin >> H && !(H == 0)) {
		for(int i = H - 1;i >= 0;i--) {
			cin >> field[0][i] >> field[1][i] >> field[2][i] >> field[3][i] >> field[4][i];
		}

		int score = 0,old_score = 0;
		do {
			old_score = score;
			for(int i = 0;i < H;i++) score += erase(i);
			
			int new_field[5][MAX_H];
			for(int i = 0;i < 5;i++) {
				for(int j = 0;j < H;j++) {
					new_field[i][j] = 0;
				}
			}
			for(int i = 0;i < 5;i++) {
				int j = 0,k = 0;
				for(int j = 0;j < H;j++) {
					if(field[i][j] != 0) new_field[i][k++] = field[i][j];
				}
			}
			
			for(int i = 0;i < 5;i++) {
				for(int j = 0;j < H;j++) {
					field[i][j] = new_field[i][j];
				}
			}
		} while(score != old_score);

		cout << score << endl;

	}
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <cmath>

using namespace std; using namespace placeholders;

using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VS = vector<string>;
using SS = stringstream;
using PII = pair<int,int>;
using VPII = vector< pair<int,int> >;
template < typename T = int > using VT = vector<T>;
template < typename T = int > using VVT = VT< VT<T> >;
template < typename T = int > using LIM = numeric_limits<T>;

template < typename T > inline T fromString( const string &s ){ T res; istringstream iss( s ); iss >> res; return res; };
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( e, c ) for ( auto &e : c )
#define ALL( c ) (c).begin(), (c).end()
#define AALL( a, t ) (t*)a, (t*)a + sizeof( a ) / sizeof( t )
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

int vanish( VVI &board )
{
	int res = 0;

	FOR( row, board )
	{
		REP( i, 0, 5 )
		{
			for ( int j = 4; i + 2 <= j; j-- )
			{
				if ( row[i] && set<int>( row.begin() + i, row.begin() + j + 1 ).size() == 1 )
				{
					res += row[i] * ( j - i + 1 );
					fill( row.begin() + i, row.begin() + j + 1, 0 );
				}
			}
		}
	}

	return res;
}

VVI fall( const VVI &board )
{
	VVI res( board.size(), VI( 5 ) );

	REP( x, 0, 5 )
	{
		VI bricks;
		REP( y, 0, board.size() )
		{
			if ( board[y][x] )
			{
				bricks.PB( board[y][x] );
			}
		}
		reverse( ALL( bricks ) );
		REP( i, 0, bricks.size() )
		{
			res[ res.size() - 1 - i ][x] = bricks[i];
		}
	}

	return res;
}

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	for ( int h; cin >> h, h; )
	{
		VVI board( h, VI( 5 ) );
		FOR( row, board )
		{
			FOR( a, row )
			{
				cin >> a;
			}
		}

		int res = 0;
		for ( int d; d = vanish( board ); res += d, board = fall( board ) );
		cout << res << endl;
	}

	return 0;
}