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 <vector>

using namespace std;

const int MAX_H = 10;
const int W = 5;
int H;

int S[MAX_H][W];

int del(){
    int ret = 0;

    for(int i=0; i<H; i++){
        vector<pair<int, int>> d;
        int cnt = 1;
        int s = 0;
        for(int j=0; j<W-1; j++){
            if(S[i][j] == S[i][j+1]){
                cnt++;
            }else{
                d.push_back(make_pair(s, cnt));
                cnt = 1;
                s = j+1;
            }
        }
        d.push_back(make_pair(s, cnt));
        for(int j=0; j<d.size(); j++){
            if(S[i][d[j].first] == -1) continue;
            if(d[j].second >= 3){
                ret += S[i][d[j].first]*d[j].second;
                for(int k=0; k<d[j].second; k++){
                    S[i][d[j].first+k] = -1;
                }
            }
        }
    }
    return ret;
}

bool move(){
    bool fl = false;
    for(int i=H-2; i>=0; i--){
        for(int j=0; j<W; j++){
            if(S[i][j] == -1) continue;
            if(S[i+1][j] == -1){
                S[i+1][j] = S[i][j];
                S[i][j] = -1;
                fl = true;
            }
        }
    }
    return fl;
}

int main(){
    vector<int> ans;
    while(true){
        cin >> H;
        if(H ==0) break;

        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                cin >> S[i][j];
            }
        }

        int ta = 0;
        while(true){
            int add = del();
            if(add == 0) break;
            ta += add;
            while(move());
        }
        ans.push_back(ta);
    }

    for(int i=0; i<ans.size(); i++){
        cout << ans[i] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

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

int main(){
    while(1){
        int h; cin>>h;
        if(h == 0) return 0;

        vector<vector<int>> num(h+10, vector<int>(5, 0));
        for(int i=0; i<h; i++){
            for(int j=0; j<5; j++){
                cin>>num[i][j];
            }
        }

        int ans=0;
        while(1){
            int sum=0;
            for(int i=h-1; i>=0; i--){
                if(num[i][0] == num[i][1] && num[i][1] == num[i][2] && num[i][2] == num[i][3] && num[i][3] == num[i][4]){
                    sum += num[i][0] * 5;
                    num[i][0] = num[i][1] = num[i][2] = num[i][3] = num[i][4] = 0;
                }
                else if(num[i][0] == num[i][1] && num[i][1] == num[i][2] && num[i][2] == num[i][3]){
                    sum += num[i][0] * 4;
                    num[i][0] = num[i][1] = num[i][2] = num[i][3] = 0;
                }
                else if(num[i][1] == num[i][2] && num[i][2] == num[i][3] && num[i][3] == num[i][4]){
                    sum += num[i][1] * 4;
                    num[i][1] = num[i][2] = num[i][3] = num[i][4] = 0;
                }
                else if(num[i][0] == num[i][1] && num[i][1] == num[i][2]){
                    sum += num[i][0] * 3;
                    num[i][0] = num[i][1] = num[i][2] = 0;
                }
                else if(num[i][1] == num[i][2] && num[i][2] == num[i][3]){
                    sum += num[i][1] * 3;
                    num[i][1] = num[i][2] = num[i][3] = 0;
                }
                else if(num[i][2] == num[i][3] && num[i][3] == num[i][4]){
                    sum += num[i][2] * 3;
                    num[i][2] = num[i][3] = num[i][4] = 0;
                }
            }

            if(sum == 0) break;

            ans += sum;

            for(int i=0; i<5; i++){
                int height = h - 1;
                for(int j=h-1; j>=0; j--){
                    if(num[j][i] != 0){
                        num[height][i] = num[j][i];
                        if(height != j) num[j][i] = 0;
                        height--;
                    }
                }
            }
        }

        cout << ans << endl;
    }
}