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;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

int main(){
    while(1){  
        int h;
        cin>>h;
        if(h==0) return 0;
        
        int table[10][5],table_cp[10][5];
        rep(i,h) rep(j,5) cin>>table[i][j];

        bool flag=true;
        int ans=0;
        while(flag){
            rep(i,h) rep(j,5) table_cp[i][j]=table[i][j];

            flag=false;

            for(int i=h;i>=0;i--){
                for(int j=1;j<4;j++){
                    if(table[i][j] && table[i][j-1]==table[i][j] && table[i][j]==table[i][j+1]){
                        flag=true;
                        table_cp[i][j-1]=0;
                        table_cp[i][j]=0;
                        table_cp[i][j+1]=0;
                    }
                }
            }

            rep(i,h) rep(j,5) ans+=table[i][j]-table_cp[i][j];
            
            int cnt=h;
            while(cnt--){
                for(int i=h-1;i>0;i--){
                    rep(j,5){
                        if(table_cp[i][j]==0){
                            table_cp[i][j]=table_cp[i-1][j];
                            table_cp[i-1][j]=0;
                        }
                    }
                }
            }
            
            rep(i,h){
                rep(j,5){
                    table[i][j]=table_cp[i][j];
                }
            }
        }

        cout<<ans<<endl;
    }
    
}

C++: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(const T a, const T b){return a>=0 ? a/b : (a+1)/b-1;}
template<class T> T div_ceil(const T a, const T b){return a>=0 ? (a-1)/b+1 : a/b;}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod*mod;
constexpr int MAX = 100010;

int n, a[10][5];

int f(){
    int ret = 0;
    rep(i, n){
        int l = 0;
        while(l < 5){
            int r = l;
            while(r < 5 && a[i][l] == a[i][r]) ++r;
            if(r-l >= 3){
                ret += a[i][l] * (r-l);
                For(j, l, r) a[i][j] = 0;
            }
            l = r;
        }
    }
    if(ret == 0) return 0;
    rrep(i, n)rep(j, 5)if(a[i][j] == 0){
        rrep(k, i)if(a[k][j]){
            a[i][j] = a[k][j];
            a[k][j] = 0;
            break;
        }
    }
    return ret;
}

int main(){
    while(scanf("%d", &n) && n){
        rep(i, n)rep(j, 5) scanf("%d", &a[i][j]);
        int ans = 0;
        while(true){
            int tmp = f();
            ans += tmp;
            if(tmp == 0) break;
        }
        printf("%d\n", ans);
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f(i,x,n) for(int i=x;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=100005,INF=1<<29;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        int H;cin>>H;
        if(H==0) break;
        
        vector<vector<int>> S(H,vector<int>(5));
        
        for(int i=0;i<H;i++){
            for(int j=0;j<5;j++){
                cin>>S[i][j];
            }
        }
        
        int score=0;
        
        while(1){
            bool ok=false;
            
            for(int i=0;i<H;i++){
                if(S[i][2]==0) continue;
                int l=2,r=2;
                while(l>=0&&S[i][l]==S[i][2]) l--;
                while(r<5&&S[i][r]==S[i][2]) r++;
                
                l++;
                r--;
                
                if(r-l>=2){
                    score+=S[i][2]*(r-l+1);
                    for(int j=l;j<=r;j++) S[i][j]=0;
                    ok=true;
                }
            }
            
            if(!ok) break;
            
            for(int k=0;k<H;k++){
                for(int i=H-1;i>0;i--){
                    for(int j=0;j<5;j++){
                        if(S[i][j]==0){
                            S[i][j]=S[i-1][j];
                            S[i-1][j]=0;
                        }
                    }
                }
            }
            
        }
        
        cout<<score<<endl;
    }
    
}