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>
using namespace std;
int main(){
int h,ans;
while(cin>>h,h){
ans=0;
int t[200][5]={{0},{0}};
for(int i=0;i<h;i++)for(int j=0;j<5;j++)cin>>t[i][j];
while(1){
bool f=1;
for(int i=0;i<h;i++){
int c=0,fi=-1;
for(int j=0;j<3;j++){
if(t[i][j]==t[i][j+1]&&t[i][j]==t[i][j+2]&&t[i][j]!=0){
c++;
if(fi==-1)fi=j;
}
}
if(c!=0)ans+=t[i][fi]*(2+c),f=0;
if(c!=0)for(int j=0;j<2+c;j++)t[i][fi+j]=0;
}
if(f)break;
for(int i=h-2;i>=0;i--){
for(int j=0;j<5;j++){
int ti=i;
while(true){
if(ti==h-1||t[ti+1][j]!=0)break;
t[ti+1][j]=t[ti][j];
t[ti][j]=0;
ti++;
}
}
}
}
cout<<ans<<endl;
}
return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
const int INF=INT_MAX,MOD=1e9+7;

int main(){
	int h;
	while(cin>>h&&h){
		int board[5][h];
		rep(i,5*h) cin>>board[i%5][h-1-i/5];
		int ans=0;
		while(1){
			bool stop=1;
			for(int high=0;high<h;high++){
				int tmp=board[0][high],cnt=1;
				for(int wide=1;wide<5;wide++){
					if(board[wide][high]==tmp){
						cnt++;
						if(wide==4&&cnt>=3&&tmp!=0){
							//cout<<tmp<<" "<<cnt<<endl;
							ans+=tmp*cnt;
							cnt--;
							while(cnt>=0){
								board[wide-cnt][high]=0;
								cnt--;
							}
							stop=0;
						}
					}else{
						if(cnt>=3&&tmp!=0){
							//cout<<tmp<<" "<<cnt<<endl;
							ans+=tmp*cnt;
							while(cnt>0){
								board[wide-cnt][high]=0;
								cnt--;
							}
							stop=0;
						}else{
							tmp=board[wide][high]; cnt=1;
						}
					}
				}
			}
			if(stop) break;
			rep(i,5){
				for(int high=0;high<h-1;high++){
					for(int y=0;y<h-1;y++){
						if(board[i][y]==0){
						 	swap(board[i][y],board[i][y+1]);
						}
					}
				}
			}
			// rep(i,h){
			// 	rep(j,5){
			// 		cout<<board[j][i]<<" ";
			// 	}
			// 	cout<<endl;
			// }
		}
		cout<<ans<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

int main(void){
	while(1){
		int h; cin>>h;
		if(!h) break;
		int ans=0, table[10][5];
		for(int i=0; i<h; i++){
			for(int j=0; j<5; j++)
				cin >>table[i][j];{
			}
		}

		int hoge = 0;
		while(1){
			for(int i=0; i<h; i++){
				int count=0;
				bool b[5];
				fill(b, b+5, false);

				if(table[i][2] == 0) continue;
				if(table[i][2] == table[i][1]){
					b[1]=true;
					count++;
					if(table[i][2] == table[i][0]){
						b[0]=true;
						count++;
					}
				}
				if(table[i][2] == table[i][3]){
					b[3]=true;
					count++;
					if(table[i][2] ==table[i][4]){
						b[4]=true;
						count++;
					}
				}

				if(count>=2){
					b[2] = true;
					count++;
					ans += (table[i][2]*count);

					for(int j=0; j<5; j++){
						if(b[j]==true){
							table[i][j]=0;
						}
					}
				}
			}//endfor

			if(hoge == ans) break;
			hoge = ans;

			for(int i=0; i<h; i++){
				for(int j=0; j<5; j++){
					if(table[i][j] == 0){
						for(int k=i; k>0; k--){
							swap(table[k][j], table[k-1][j]);
						}
					}
				}
			}
		}//endwhile
		cout << ans << endl;
	}//endwhile
	return 0;
}

C++: #include <vector>
#include <iostream>

#pragma warning(disable : 4996)

using namespace std;

int main()
{
	int H;

	while (true)
	{
		scanf("%d", &H);

		if (H == 0) { break; }

		vector<vector<int> > M(H, vector<int>(5));

		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < 5; j++)
			{
				scanf("%d", &M[i][j]);
			}
		}

		int ret = 0;

		while (true)
		{
			for (int i = 0; i < H; i++)
			{
				for (int j = 5; j >= 3; j--)
				{
					for (int k = 0; k <= 5 - j; k++)
					{
						bool ok = (M[i][k] != 0);

						for (int l = k + 1; l < k + j; l++)
						{
							if (M[i][l - 1] != M[i][l] || M[i][l] == 0)
							{
								ok = false;
							}
						}

						if (ok)
						{
							ret += M[i][k] * j;

							for (int l = k; l < k + j; l++)
							{
								M[i][l] = 0;
							}
						}
					}
				}
			}

			bool ok = false;

			for (int rep = 0; rep < H; rep++)
			{
				for (int i = H - 1; i >= 1; i--)
				{
					for (int j = 0; j < 5; j++)
					{
						if (M[i][j] == 0 && M[i - 1][j] != 0)
						{
							M[i][j] = M[i - 1][j]; M[i - 1][j] = 0; ok = true;
						}
					}
				}
			}

			if (!ok) { break; }
		}

		printf("%d\n", ret);
	}

	return 0;
}