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 <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

int h;
int grid[10][5];

int getPoint() {
    int point = 0;
    for (int y = 0; y < h; ++y) {
        for (int x = 0; x < 5; ++x) {
            int cnt = 0;
            int tnt = grid[y][x];
            while (grid[y][x] == tnt && x < 5) {
                cnt++;
                x++;
            }
            x--;
            if (cnt > 2) {
                point += cnt * tnt;
                for (int c = 0; c < cnt; ++c ) {
                    grid[y][x-c] = 0;
                }
            }
        }
    }
    return point;
}

void deleteBlock() {
    for (int x = 0; x < 5; ++x) {
        bool flag = true;
        while (flag) {
            flag = false;
            for (int y = h - 1; y > 0; --y) {
                if (grid[y][x] == 0 && grid[y-1][x] > 0) {
                    swap(grid[y][x], grid[y-1][x]);
                    flag = true;
                }
            }
        }
    }
}

int solve() {
    int point = 0;
    while (1) {
        int tmp = getPoint();
        if (tmp == 0) break;
        point += tmp;
        deleteBlock();
    }
    return point;
}

int main (int argc, char *argv[]) {
    while (cin>>h, h) {
        for (int y = 0; y < h; ++y) {
            for (int x = 0; x < 5; ++x) {
                cin>>grid[y][x];
            }
        }
        cout<<solve()<<endl;
    }
    return 0;
}

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

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

using namespace std;

static const ll INF = 1e15;

template<typename T>
static inline void chmin(T & ref, const T  value) {
    if (ref > value) ref = value;
}

template<typename T>
static inline void chmax(T & ref, const T value) {
    if (ref < value) ref = value;
}

const ll mod = 1000000007;


int main() {
	while (true) {
		int n;
		cin >> n;
		if (n == 0) return 0;
		vector<vector<int>> grid(n, vector<int>(5, 0));
		rep(i, n) {
			rep(j, 5) {
				cin >> grid[i][j];
			}			
		}
		bool updated = true;
		ll score = 0;
		while (updated) {
			updated = false;
			vector<vector<bool>> temp(n, vector<bool>(5, false));
			rep(i, n) {
				rep(j, 3) {
					int color = grid[i][j];
					if (color == -1) continue;
					bool match = true;
					for (int k = 1; k < 3 && j + k < 5; k++) {
						if (grid[i][j + k] != color) {
							match = false;
							break;
						}
					}
					if (match) {
						updated = true;
						rep(k, 3) {
							temp[i][j + k] = true;
						}
					}
				}
			}
			rep(i, n) rep(j, 5) {
				if (temp[i][j]) {
					score += grid[i][j];
				}
			}
			vector<vector<int>> nextGrid(n, vector<int>(5, -1));
			rep(j, 5) {
				int target = n - 1;
				for (int i = n - 1; i >= 0; i--) {
					while(target >= 0 && temp[target][j]) {
						target--;
					}
					if (target < 0) break;
					nextGrid[i][j] = grid[target][j];
					target--;
				}
			}
			swap(grid, nextGrid);
		}
		cout << score << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string.h>

int H,W=5,B[11][5],i,j,R;

void dbg()
{
	int i,j;
	for(i=0;i<H;i++,puts(""))
		for(j=0;j<W;j++)
			printf("%2d",B[i][j]);
	puts("");
}

int del()
{
	int i,j,k,t,r=0;
	for(int i=0;i<H;i++)
		for(j=0;j<W;j++)
		{
			if(B[i][j]!=-1)
			{
				for(k=j+1;j<W&&B[i][j]==B[i][k];k++);
				if(k-j>=3)
				{
					r+=B[i][j]*(k-j);
					for(t=j;t<k;t++)B[i][t]=-1;
				}
			}
		}
	R+=r;
	return r;
}

void fall()
{
	int i,j,k,t;
	for(i=0;i<W;i++)
		for(j=H-1;j>=0;j--)
			if(B[j][i]!=-1)
			{
				for(k=j+1;k<H&&B[k][i]==-1;k++);
				k--;
				t=B[k][i];
				B[k][i]=B[j][i];
				B[j][i]=t;
			}
}

int main()
{
	for(;scanf("%d",&H),H;)
	{
		R=0;
		for(i=0;i<H;i++)
			for(j=0;j<5;j++)
				scanf("%d",&B[i][j]);
		//dbg();
		while(del()!=0)
		{
			fall();
		}
		printf("%d\n",R);
	}
}

C++: #include <cstdio>
using namespace std;

int main() {
	const int MAX_H = 10;
	int H, board[MAX_H][5];
	int res;
	bool updated;
	bool zeroseq;
	int seq;

	while(true){
		scanf("%d", &H);
		if (H == 0) {
			break;
		}
		for (int i = 0;i < H;i++) {
			for (int j = 0;j < 5;j++) {
				scanf("%d", &board[i][j]);
			}
		}

		updated = true;
		res = 0;
		while (updated) {
			updated = false;
			for (int i = 0;i < H;i++) {
				seq = 1;
				for (int j = 0;j < 5;j++) {
					if (j == 4 || board[i][j] != board[i][j + 1]) {
						if (seq >= 3) {
							updated = true;
							res += board[i][j] * seq;
							for (int k = 0;k < seq;k++) {
								board[i][j - k] = 0;
							}
						}
						seq = 1;
					}
					else if (j + 1 < 5 && board[i][j] == board[i][j + 1] && board[i][j] != 0) {
						seq++;
					}
				}
			}
			for (int j = 0;j < 5;j++) {
				zeroseq = true;
				for (int i = 0;i < H;i++) {
					if (board[i][j] == 0 && !zeroseq) {
						for (int k = i;k > 0;k--) {
							board[k][j] = board[k - 1][j];
						}
						board[0][j] = 0;
					}
					else {
						zeroseq = false;
					}
				}
			}
		}

		printf("%d\n", res);
	}

	return 0;
}