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>
#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;
 
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};


bool ch[7][15];
int flag,ans;
int mini[6];
int fil[7][7] = {
    {0,1,1,1,1,1},
    {0,1,1,1,1,0},
    {0,0,1,1,1,1},
    {0,1,1,1,0,0},
    {0,0,1,1,1,0},
    {0,0,0,1,1,1},
};
int fil2[] = {5,4,4,3,3,3};

int main(){
    int h;
    while(true){
        cin >> h;
        if(h == 0) break;
        vector<int> v[500];
        for(int i = 0;i < h;i++){
            for(int j = 1;j <= 5;j++){
                int tmp;
                cin >> tmp;
                v[j].insert(v[j].begin(), tmp);
            }
        }

        ans = 0;
        for(int num = 0;true;num++){
            int tmpans = ans;
            for(int i = 0;i <= h;i++){
                for(int j = 1;j <= 5;j++){
                    ch[j][i] = false;
                }
            }
            for(int i = 0;i < 6;i++){
                mini[i] = v[num*5+3].size();
                for(int j = 1;j <= 5;j++){
                    if(fil[i][j] == 0)continue;
                    if(mini[i] > v[num*5+j].size()) mini[i] =v[num*5+j].size();
            //        mini[i] = min(mini[i],v[num*5+j].size());
                }
            }

            for(int i = 0;i < h;i++){
                for(int k = 0;k < 6;k++){
                    if(i > mini[k]-1) continue;
                    int same = v[num*5+3][i];
                    int flag = 1;
                    for(int j = 1;j <= 5;j++){
                        if(fil[k][j] == 0)continue;
                        if(same != v[num*5+j][i]){
                            flag = 0;
                            break;
                        }
                    }
                    if(flag == 1){
                        ans += same*fil2[k];
                        for(int j = 1;j <= 5;j++){
                            if(fil[k][j] == 1)ch[j][i] = true;
                        }
                        break;
                    }
                }
            }
            for(int i = 1;i <= 5;i++){
                for(int j = 0;j < v[num*5+i].size();j++){
                    if(ch[i][j] == false) v[num*5+5+i].push_back(v[num*5+i][j]);
                }
            }
            if(tmpans == ans)break;
        }
        cout << ans << endl;

    }

}

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

using namespace std;

int main(){
	ios_base::sync_with_stdio(false);
	while(true){
		int h, w = 5;
		cin >> h;
		if(h == 0){ break; }
		vector<vector<int>> field(h, vector<int>(w));
		for(int i = 0; i < h; ++i){
			for(int j = 0; j < w; ++j){ cin >> field[i][j]; }
		}
		int score = 0;
		while(true){
			bool modified = false;
			for(int i = 0; i < h; ++i){
				for(int j = 0; j < w; ++j){
					if(field[i][j] < 0){ continue; }
					int k = j;
					while(k < w && field[i][j] == field[i][k]){ ++k; }
					if(k - j < 3){ continue; }
					for(--k; k >= j; --k){
						score += field[i][k];
						field[i][k] = -1;
						modified = true;
					}
				}
			}
			if(!modified){ break; }
			for(int j = 0; j < w; ++j){
				int k = h - 1;
				for(int i = h - 1; i >= 0; --i){
					if(field[i][j] >= 0){ field[k--][j] = field[i][j]; }
				}
				for(; k >= 0; --k){ field[k][j] = -1; }
			}
		}
		cout << score << endl;
	}
	return 0;
}

C++: #include "iostream"
#include "random"
#include "ctime"
#include "chrono"
#include "string"
#include "bitset"
#include "algorithm"
#include "map"
#include "queue"
#include "list"

using namespace std;


int main() {
	
	int H;
	const int W = 5;
	cin >> H;
	list<int>ret;
	while (H) {
		vector<vector<int>>v(H, vector<int>(W));
		int ans = 0;
		for (int i = H - 1; i >= 0; i--) {
			for (int j = 0; j < W; j++) {
				cin >> v[i][j];
			}
		}
		bool flag = true;
		while (flag) {
			flag = false;
			for (int i = 0; i < H; i++) {
				vector<int>num(10,0);
				for (int j = 0; j < W; j++) {
					num[v[i][j]]++;
				}
				for (int j = 1; j < 10; j++) {
					while (num[j] >= 3) {
						for (int k = 0; k <= W - num[j]; k++) {
							int cnt = 0;
							for (int l = k; l < k + num[j]; l++) {
								cnt += j == v[i][l];
							}
							if (cnt == num[j]) {
								flag = true;
								ans += j*num[j];
								for (int k = 0; k < W; k++) {
									if (v[i][k] == j) {
										v[i][k] = 0;
									}
								}
							}
						}
						num[j]--;
					}
				}
			}
			for (int i = 1; i < H; i++) {
				for (int j = 0; j < W; j++){
					if (v[i][j]) {
						for (int k = i; k > 0; k--) {
							if (!v[k - 1][j]) {
								swap(v[k][j], v[k - 1][j]);
							}
						}
					}
				}
			}
		}
		ret.push_back(ans);
		cin >> H;
	}
	for (auto i : ret) {
		cout << i << endl;
	}
	return 0;
}