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 inf = 1e9 + 7;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
const int mod = 1e9 + 7;
ll power(ll a,ll b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}

int main() {
    int n;
    while (cin >> n && n) {
        vector<int> v[5];
        rep(i, n) rep(j, 5) {int p; cin >> p; v[j].push_back(p);}
        int flg = 0;
        int sum = 0;
        //cout << endl; rep(i, n) {rep(j, 5) cout << v[j][i] << " "; cout << endl;}
        while (1) {
            flg = 0;
            rep(i, n) { //まず全部の0
                int c = 0, key = -1;
                rrep(j, 2, 5) { //vector
                    if (v[j][i] == v[j - 1][i] && v[j][i] == v[j - 2][i] && v[j][i] != 0) {
                        c++;
                        key = v[j][i];
                    }
                }
                //cout << key << endl;
                if (c) {
                    flg = 1;
                    rep(j, 5) {
                        if (v[j][i] == key) {
                            sum += key;
                            v[j][i] = 0;
                        }
                    }
                }
            }
            if (n == 1) {cout << sum << endl; flg = 0; break;}
            //cout << endl; rep(i, n) {rep(j, n) cout << v[j][i] << " "; cout << endl;}
            //slide phase
            rep(i, n) {
                rep(j, 5) {
                    if (v[j][i] == 0) {
                        for (int k = i; k >= 1; k--) {
                            v[j][k] = v[j][k - 1];
                        }
                        v[j][0] = 0;
                    }
                }
            }
            //cout << endl; rep(i, n) {rep(j, n) cout << v[j][i] << " "; cout << endl;}
            if (flg == 0) break;
        }
        if (n != 1) cout << sum << endl;
    }
}

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

int main()
{
vector<vector<int>> board;
vector<vector<bool>> check;
vector<int> count;
int H,ans=0;bool c=true;scanf("%d",&H);
while(H!=0)
{
ans=0;c=true;
board.resize(0);check.resize(0);board.resize(H);check.resize(H);
for(int i=0;i<H;i++){board[i].resize(5);check[i].resize(5,false);}
for(int i=0;i<H;i++)for(int j=0;j<5;j++)scanf("%d ",&board[i][j]);
while(c)
{
c=false;
for(int i=0;i<H;i++)
{
count.resize(0);count.resize(5,1);
for(int j=0;j<5;j++){if(check[i][j]){count[j]=0;continue;}if(j==0)continue;if(board[i][j-1]==board[i][j])count[j]+=count[j-1];}
for(int j=4;j>-1;j--)if(count[j]>2&&!check[i][j]){ans+=count[j]*board[i][j];for(int k=j;k>j-count[j];k--)check[i][k]=true;c=true;break;}
}
for(int j=0;j<5;j++)
{
  int stack=H-1;
  for(int i=H-1;i>-1;i--)if(!check[i][j]){board[stack][j]=board[i][j];check[i][j]=true;check[stack][j]=false;stack--;}
}
}
printf("%d\n",ans);
scanf("%d",&H);
}
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <complex>
#include <string>
#include <sstream>
#include <iomanip>
#include <climits>
#include <functional>
#include <bitset>
#include <tuple>

using namespace std;

typedef long long ll;

int H;
const int W = 5;

int Dfs(int y, int x, int c, vector<vector<int>> &board) {
    if (x >= W || board[y][x] != c)
        return 0;
    return Dfs(y, x + 1, c, board) + 1;
}

int main()
{
    while (cin >> H, H) {
        vector<vector<int>> board(H, vector<int>(W, 0));

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

        int ans = 0;
        bool change = true;

        while (change) {
            change = false;

            for (int i = 0; i < H; ++i) {
                for (int j = 0; j < W; ++j) {
                    if (board[i][j] == 0)
                        continue;
                    int cnt = Dfs(i, j, board[i][j], board);
                    if (cnt >= 3) {
                        ans += cnt * board[i][j];
                        for (int k = 0; k < cnt; ++k)
                            board[i][j + k] = 0;
                        change = true;
                    }
                }
            }

            if (!change)
                break;

            for (int j = 0; j < W; ++j) {
                for (int i = H - 1; i >= 0; --i) {
                    if (board[i][j] == 0) {
                        int k = i - 1;
                        while (k >= 0 && board[k][j] == 0)
                            --k;
                        if (k < 0)
                            break;
                        swap(board[i][j], board[k][j]);
                    }
                }
            }
        }

        cout << ans << endl;
    }


    return 0;
}