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<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<functional>
#include<map>
#include<unordered_set>
using namespace std;
/*int p = 998244353;*/
int p = 1000000007;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define qin pair<pin,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define sq(a) (a)*(a)
#define rev(s) reverse(s.begin(),s.end())
#define end_pr(s) cout << s <<endl;return 0
#define itn int
int RE() {
	vel v(3, 2);
	return v.at(4);
}
int ru(int a, int r) {
	if (r == 0) { return 1; }
	int ans = ru(a, r / 2);
	ans *= ans; ans %= p;
	if (r % 2 == 1) { ans *= a; }
	return ans % p;
}
int inv(int a) {
	return ru(a,p - 2);
}
vel dis(int mid1, vvel &way) {
	int n = way.size();
	vel dist(n, -1); dist[mid1] = 0;
	queue<int> q;
	q.push(mid1);
	while (!q.empty()) {
		int st = q.front(); q.pop();
		rep(i, way[st].size()) {
			int to = way[st][i];
			if (dist[to] == -1) {
				dist[to] = dist[st] + 1;
				q.push(to);
			}
		}
	}
	return dist;
}
pin most_far(int now, int n, vvel &way) {
	vel dist1 = dis(now, way);
	pin ans = mkp(-1, 0);
	rep(i, n) {
		if (dist1[i] > ans.first) { ans = mkp(dist1[i], i); }
	}
	return ans;
}

template<typename T>
V<T> uni(V<T> &v) {
	if (v.size() == 0) { return v; }
	sor(v);
	V<T> ans(1, v[0]);
	for (int i = 1; i < v.size(); i++) {
		if (v[i] != v[i - 1]) { ans.push_back(v[i]); }
	}
	v = ans;
	return v;
}
int s_gcd(int a, int b) {
	if (b == 0) { return a; }
	return s_gcd(b, a%b);
}
int gcd(int a, int b) {
	a = abs(a);
	b = abs(b);
	if (a < b) { swap(a, b); }
	return s_gcd(a, b);
}
vel dx{ 1,1,0,0,-1,-1 };
vel dy{ 0,1,1,-1,-1,0 };
signed main() {
	while (true) {
		int h; cin >> h;
		if (h == 0) { break; }
		int ans = 0;
		vvel pla(h, vel(5));
		rep(i, h) {
			rep(j, 5) { cin >> pla[i][j]; }
		}
		rev(pla);
		while (true) {
			int diff = 0;
			rep(i, h) {
				if (pla[i][2] != -1) {
					int count = 1;
					if (pla[i][1] == pla[i][2]) {
						count++;
						if (pla[i][0] == pla[i][2]) { count++; }
					}
					if (pla[i][3] == pla[i][2]) {
						count++;
						if (pla[i][4] == pla[i][2]) { count++; }
					}
					if (count >= 3) {
						diff += pla[i][2] * count;
						if (pla[i][1] == pla[i][2]) {
							pla[i][1] = -1;
							if (pla[i][0] == pla[i][2]) { pla[i][0] = -1;; }
						}
						if (pla[i][3] == pla[i][2]) {
							pla[i][3] = -1;
							if (pla[i][4] == pla[i][2]) { pla[i][4] = -1; }
						}
						pla[i][2] = -1;
					}
				}
			}
			if (diff == 0) { break; }
			rep(i, 5) {
				int back = 0;
				rep(j, h) {
					if (pla[j][i] != -1) {
						pla[back][i] = pla[j][i];
						back++;
					}
				}
				for (int j = back; j < h; j++) { pla[j][i] = -1; }
			}
			ans += diff;
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

int d[11][6];
bool check[11][6];
int H;

void write() {
    rep(i, H) {
        rep(j, 5) cout << d[i][j] << " ";
        cout << endl;
    }
    cout << endl;
}
void fall() {
    rep(i, H) rep(j, 5) if (check[i][j]) d[i][j] = 0;
    repr(i, H) {
        rep(j, 5) {
            if (d[i][j] == 0) {
                bool update = true;
                while (update && d[i][j] == 0) {
                    update = false;
                    for (int k = i - 1; k >= 0; --k) {
                        d[k + 1][j] = d[k][j];
                        if(d[k][j] > 0) update = true;
                    }
                    d[0][j] = 0;
                }
            }
        }
    }
    // write();
}
int erase() {
    rep(i, 11) rep(j, 6) check[i][j] = 0;
    rep(i, H) {
        rep(j, 3) {
            if (d[i][j] > 0 && d[i][j] == d[i][j + 1] && d[i][j] == d[i][j + 2]) {
                check[i][j] = check[i][j + 1] = check[i][j + 2] = true;
            }
        }
    }
    int ret = 0;
    rep(i, H) rep(j, 5) if (check[i][j]) ret += d[i][j];
    fall();
    return ret;
}

int main() {
    while (cin >> H) {
        if (H == 0) break;
        rep(i, H) {
            rep(j, 5) cin >> d[i][j];
        }
        int s = 0;
        while (1) {
            int t = erase();

            if (t == 0) break;
            s += t;
        }
        cout << s << endl;
    }
    return 0;
}