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;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i=(a);i<(b);i++)
#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)
#define REP(i,n) for (ll i=0;i<(n);i++)
#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)
#define debug(x) cout<<#x<<": "<<x<<endl
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
	EACH(x,vec) is >> x;
	return is;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
	REP(i,vec.size()) {
		if (i) os << " ";
		os << vec[i];
	}
	return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector< vector<T> >& vec) {
	REP(i,vec.size()) {
		if (i) os << endl;
		os << vec[i];
	}
	return os;
}

const int W = 5;
int H;
vector< vector<int> > m;
ll check() {
	ll res = 0;
	// erase and add score
	REP(y, H) {
		REP(x, W) {
			int l = x, r = x;
			FOR(i, x, W) {
				if (m[y][x] != m[y][i]) break;
				r = i;
			}
			++r;
			if (r-l < 3) continue;
			res += (ll)(r-l) * m[y][x];
			FOR(i, x, r) {
				m[y][i] = 0;
			}
		}
	}
//	cout << m << endl;
//	cout << endl;
	// fall
	REP(t, H) {
		RFOR(y, 1, H) {
			REP(x, W) {
				if (m[y][x] == 0) {
					m[y][x] = m[y-1][x];
					m[y-1][x] = 0;
				}
			}
		}
	}
//	cout << m << endl;
//	cout << endl;
	return res;
}
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	while (cin >> H, H) {
		m = vector< vector<int> >(H, vector<int>(W, 0));
		cin >> m;
		ll ans = 0;
		while (1) {
			int c = check();
			ans += c;
			if (c == 0) break;
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <stack>
#include <cmath>
using namespace std;

int H, score;
int board[10][5];

bool checkBoard(){
	bool flag = false;
	for(int i = 0; i < H; i++){
		int prev = board[i][0], head = 0, tail = 1;
		for(int j = 1; j < 5; j++){
			if(board[i][j] == prev && prev != 0) tail++;
			else{
				if(tail - head >= 3) continue;
				head = j;
				tail = head + 1;
				prev = board[i][j];
			}
		}

		if(tail - head < 3) continue;
		score += board[i][head] * (tail - head);
		flag = true;
		
		for(int j = i; j >= 1; j--){
			for(int s = head; s < tail; s++) board[j][s] = board[j-1][s];
		}

		for(int j = head; j < tail; j++) board[0][j] = 0;
	}

	return flag;
}

int main(void)
{
	while(cin >> H && H != 0){
		score = 0;
		for(int i = 0; i < H; i++){
			for(int j = 0; j < 5; j++) cin >> board[i][j];
		}
		while(checkBoard());
		cout << score << endl;
	}
	return 0;
}

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

int read_table(vector<vector<int>> &t);
int calc_score(vector<vector<int>> &t);
int print_table(vector<vector<int>> &t);

int main()
{

	int h;
	vector<vector<int>> t(10,vector<int>(5));
	
	for (;;)
	{
		h=read_table(t);
		if (h==0)
		{
			return 0;
		}

		int total_score=0;
		for (;;)
		{
			//print_table(t);
			int score=calc_score(t);
			//fprintf(stderr, "score: %d\n",score);
			if (score==0)
			{
				cout << total_score << endl;
				break;
			}
			total_score+=score;
		}
	}
}

int read_table(vector<vector<int>> &t)
{
	int h;
	cin >> h;
	if (h==0)
	{
		return 0;
	}

	int y;
	for (y=0;y<10-h;++y)
	{
		for (int j=0;j<5;++j)
		{
			t[y][j]=0;
		}
	}
	for (int i=0;i<h;++i)
	{
		y=(10-h)+i;
		for (int j=0;j<5;++j)
		{
			cin >> t[y][j];
		}
	}

	return h;
}


int calc_score(vector<vector<int>> &t)
{
	int total_score=0;
 	const int multiplier[]={
		0, 0, 0, 0, 0, 0, 0, 3,
		0, 0, 0, 0, 0, 0, 3, 4,
		0, 0, 0, 0, 0, 0, 0, 3,
		0, 0, 0, 0, 3, 3, 4, 5,
	};

 	const int mark[]={
		0, 0, 0, 0, 0, 0, 0, 7,
		0, 0, 0, 0, 0, 0, 14, 15,
		0, 0, 0, 0, 0, 0, 0, 7,
		0, 0, 0, 0, 28, 28, 30, 31,
	};


	// calc score and mark scored stones
	for (int i=10-1;i>=0;--i)
	{
		int s2=t[i][2];
		int p=4;
		if (t[i][0]==s2)
		{
			p+=1;
		}
		if (t[i][1]==s2)
		{
			p+=2;
		}
		if (t[i][3]==s2)
		{
			p+=8;
		}
		if (t[i][4]==s2)
		{
			p+=16;
		}
		//fprintf(stderr,"%d %d\n",i,p);
		total_score+=multiplier[p]*s2;

		if ((mark[p]&1)!=0)
		{
			t[i][0]=0;
		}
		if ((mark[p]&2)!=0)
		{
			t[i][1]=0;
		}
		if ((mark[p]&4)!=0)
		{
			t[i][2]=0;
		}
		if ((mark[p]&8)!=0)
		{
			t[i][3]=0;
		}
		if ((mark[p]&16)!=0)
		{
			t[i][4]=0;
		}
	}

	// shift stone down
	for (int i=0;i<5;++i)
	{
		int current=10-1;
		int lowest=current;
		while (current>=0)
		{
			while ((lowest>=0)&&(t[lowest][i]==0))
			{
				--lowest;
			}
			if (lowest<0)
			{
				// no more stone...
				for (;current>=0;--current)
				{
					t[current][i]=0;
				}
			}
			else
			{
				t[current][i]=t[lowest][i];
				--current;
				--lowest;
			}
		}
	}

	return total_score;
}


int print_table(vector<vector<int>> &t)
{
	fprintf(stderr,"table:\n");
	for (int i=0;i<10;++i)
	{
		for (int j=0;j<5;++j)
		{
			fprintf(stderr," %d",t[i][j]);
		}
		fprintf(stderr,"\n");
	}
	//fprintf(stderr,"\n");
}