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;
using vi=vector<int>;
using vvi=vector<vi>;
using vs=vector<string>;
using msi=map<string,int>;
using mii=map<int,int>;
using pii=pair<int,int>;
using vlai=valarray<int>;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second

int main(){
	int n;
	while(cin>>n,n){
		vvi v(n,vi(5));
		rep(i,n)rep(j,5)cin>>v[i][j];
		int ans=0;
		bool isC=true;
		while(isC){
	        isC=false;
    		rep(i,n){
    		    for(vi p : vvi{{0,5},{0,4},{1,5},{0,3},{1,4},{2,5}}){
        		    if(v[i][p[0]]!=0 && count(v[i].begin()+p[0], v[i].begin()+p[1], v[i][p[0]]) == p[1]-p[0]){
        		        isC=true;
        		        ans+=(p[1]-p[0])*v[i][p[0]];
        		        fill(v[i].begin()+p[0], v[i].begin()+p[1], 0);
        		        break;
        		    }
    		    }
    		}
    		rep(lp,n){
        		for(int i=n-1;i>=1;i--)rep(j,5){
        		    if(v[i][j]==0) swap(v[i-1][j], v[i][j]);
        		}
    		}
    		//rep(i,n){rep(j,5)cout<<v[i][j];cout<<endl;}
		}
	    cout<<ans<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<cstdio>
#include<string>
#include<utility>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 2e9;
ll N,H,W,a,b,c,d,e;

ll A[20][5] = {};
bool f,ff;

queue<ll> qll[6];

int main() {

	while (true) {
		cin >> H;
		if (H == 0) {
			break;
		}
		e = 0;

		for (int i = 0; i < 20; i++) {
			for (int j = 0; j < 5; j++) {
				A[i][j] = -1;
			}
		}

		for (int i = 0; i < H; i++) {
			for (int j = 0; j < 5; j++) {
				cin >> A[H-1-i][4-j];
			}
		}

		d = H * 5;

		while (d > 0) {
			ff = true;
			for (int i = 0; i < H; i++) {

				a = 1;
				b = A[i][0];

				for (int j = 1; j < 5; j++) {
					if (A[i][j] == b && b > 0) {
						a++;
					}
					else {
						a = 1;
						b = A[i][j];
					}

					if (a >= 3) {
						if (j == 4) {
							e += a * b;
							d -= a;
							ff = false;
							for (int k = 0; k < a; k++) {
								A[i][j - k] = -1;
							}
							break;
						}
						else if (A[i][j + 1] != b) {
							e += a * b;
							d -= a;
							ff = false;
							for (int k = 0; k < a; k++) {
								A[i][j - k] = -1;
							}
							break;
						}
					}


				}
			}

			f = true;

			for (int i = 0; i < H; i++) {
				for (int j = 0; j < 5; j++) {
					if (A[i][j] != -1) {
						f = false;
					}
				}
			}

			if (f || ff)break;


			for (int j = 0; j < 5; j++) {
				for (int i = 0; i < H; i++) {
					if (A[i][j] != -1) {
						qll[j].push(A[i][j]);
					}
				}
			}

			for (int i = 0; i < 20; i++) {
				for (int j = 0; j < 5; j++) {
					A[i][j] = -1;
				}
			}

			for (int j = 0; j < 5; j++) {
				W = 0;
				while (!qll[j].empty())
				{
					A[W][j] = qll[j].front();
					W++;
					qll[j].pop();
				}
			}

		}

		cout << e << endl;

	}

	

}

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

vector<int> c[5];

int erase(int h) {
	int ct = 1,n = 0,old_n = 0,max_ct = 1,i2,n2;
	for(int i = 0;i < 5;i++) {
		old_n = n;
		if((int)c[i].size() > h) n = c[i][h];
		else n = 0;

		if(old_n == n) ct++;
		else ct = 1;

		if(ct > max_ct) {
			max_ct = ct;
			i2 = i;
			n2 = n;
		}
	}
	if(max_ct >= 3 && n2 != 0) {
		for(int i = i2 - max_ct + 1;i <= i2;i++) c[i][h] = 0;
		return max_ct * n2;
	}
	return 0;
}

int main() {

	int h;
	while(cin >> h && h != 0) {
		for(int i = 0;i < 5;i++) {
			c[i].resize(h,0);
		}
		for(int i = 0;i < h;i++) {
			cin >> c[0][h - i - 1] >> c[1][h - i - 1] >> c[2][h - i - 1] >> c[3][h - i - 1] >> c[4][h - i - 1];
		}

		int score = 0,old_score = -1;
		while(score != old_score) {
			old_score = score;
			for(int i = 0;i < h;i++) score += erase(i);
			for(int i = 0;i < 5;i++) c[i].erase(remove(c[i].begin(),c[i].end(),0),c[i].end());
		}

		cout << score << endl;
	}
	 
}