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++: hogehoge

C++: #include<iostream>
#include<cstring>
using namespace std;

int t[25][6];

int main(){
	int n,m;
	int i,j,k;
	int a,b,c;
	int s=0;
	bool flag;
	while(cin>>n){
		if(!n)break;
		memset(t,0,sizeof(t));
		for(i=n-1;i>=0;i--){
			for(j=0;j<5;j++)cin>>t[i][j];
		}
		flag=true,s=0;
		while(flag){
			flag=false;
			for(i=0;i<n;i++){
				for(j=0,k=1;j<4;j++){
					if(t[i][j]==t[i][j+1])k++;
					else{
						if(k>=3 && t[i][j]!=0){
							s+=t[i][j]*k;
							for(;k>0;k--)t[i][j-k+1]=0;
							flag=true;
						}
						k=1;
					}
				}
				if(k>=3 && t[i][j]!=0){
					s+=t[i][j]*k;
					for(;k>0;k--)t[i][j-k+1]=0;
					flag=true;
				}
			}
			for(i=0;i<5;i++){
				for(j=0,k=0;j<n;j++){
					if(t[j][i]==0)continue;
					t[k][i]=t[j][i],k++;
				}
				for(;k<n;k++)t[k][i]=0;
			}
		}
		cout<<s<<endl;
	}
}

C++: #include <cstdio>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int main(){
	for(int h,w=5;scanf("%d",&h),h;){
		int B[10][5];
		rep(i,h) rep(j,w) scanf("%d",B[i]+j);

		int ans=0;
		while(1){
			bool changed=false;

			// vanish
			for(int i=h-1;i>=0;i--){
				int cont=1;
				for(int j=1;j<=w;j++){
					if(j==w || B[i][j-1]!=B[i][j]){
						if(cont>=3 && B[i][j-1]!=0){
							ans+=B[i][j-1]*cont;
							rep(k,cont) B[i][j-k-1]=0;
							changed=true;
						}
						cont=1;
					}
					else cont++;
				}
			}

			// fall
			rep(j,w){
				rep(_,h) rep(i,h-1) {
					if(B[i][j]!=0 && B[i+1][j]==0) B[i+1][j]=B[i][j], B[i][j]=0;
				}
			}

			if(!changed) break;
		}
		printf("%d\n",ans);
	}

	return 0;
}

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

int H;

void debugprint(vector<vector<int>> &data) {
    printf("\n");
    printf("\n");
    for(int i=0; i<H; i++) {
    for(int j=0; j<5; j++) {
        printf("%d ", data[i][j]);
    }
    printf("\n");
    }
    printf("\n");
    printf("\n");
}

void data_swap(vector<vector<int>> &data, int h) {
    for(int i=0; i<H-1; i++) {
        for(int j=0; j<5; j++) {
            if(data[i+1][j]==-1) {
                for(int k=i; k>=0; k--) {
                    swap(data[k][j], data[k+1][j]);
                }
            }
        }
    }
}

bool check(vector<vector<int>> &data, int h, int &score) {
    for(int i=0; i<3; i++) {
        int tmp = data[h][i];
        if( tmp != -1 &&
           (data[h][i] == data[h][i+1] && data[h][i] == data[h][i+2])) {
            
            
            for(int j=i; j<5; j++) {
                if(tmp == data[h][j]) {
                    data[h][j] = -1;
                    score += tmp;
                } else {
                    break;
                }
            }
            return true;
        }
    }
    return false;
}

bool solve() {
    cin >> H;
    if(H==0) return false;

    vector<vector<int>> data;
    data.resize(H);
    for(int h=0; h<H; h++) {
        for(int w=0; w<5; w++) {
            int tmp; cin >> tmp;
            data[h].push_back(tmp);
        }
    }

    int score = 0;
    RETRY:
    bool retry = false;
    for(int h=H-1; h>=0; h--) {
        retry |= check(data, h, score);
    }
    data_swap(data, 0);
    if (retry) goto RETRY;

    cout << score << endl;

    return true;
}

int main() {
    while(solve()) {}
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>
#include <stack>
#include <tuple>

#define ll long long int

using namespace std;

const int INF = (1<<30) - 1;


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int H;
    while (cin>>H, H) {
        vector<vector<int>> field(H, vector<int>(5));
        for(int i=0; i<H; i++)
            for(int j=0; j<5; j++)
                cin >> field[i][j];
        
        int ans = 0;
        bool updated = true;
        while (updated) {
            updated = false;
            for (int i=0; i<H; i++) {
                int c = 1;
                int a = 0, b = 0;
                for(int j=1; j<5; j++){
                    if(field[i][j] != 0 && field[i][j-1] == field[i][j])
                        c++;
                    else
                        c = 1;
                    if(c >= 3){
                        b = j + 1;
                        a = b- c;
                        updated = true;
                    }
                }
                for(int j=a; j<b; j++){
                    ans += field[i][j];
                    field[i][j] = 0;
                }
            }
            
            for(int i=0; i<5; i++){
                int index = H-1;
                for(int j=H-1; 0<=j; j--){
                    while(index >= 0 && field[index][i] == 0)
                        index--;
                    
                    field[j][i] = (index < 0 ? 0 : field[index--][i]);
                }
            }
        }
        cout << ans << endl;
    }
    
    return 0;
}