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 <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

#define INF (int)1e09

using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::fixed;
using std::pair;
using std::make_pair;
using std::min;
using std::max;
using std::string;
using std::vector;
using std::list;
using std::map;
using std::set; // insert erase find count
using std::queue;
using std::priority_queue;
using std::stack;

template <typename T> using Vec = vector<T>; // c++11
typedef pair<int, int> PII;
typedef pair<int, pair<int, int> > PIII;

void dump(Vec<Vec<int> >& table, int h);

int del(Vec<Vec<int> >& table, int h) {
    int score = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j <= 2; j++) {
            int end = j + 1;
            while (1) {
                if (table[i][j] == table[i][end]) {
                    end++;
                } else {
                    end--;
                    break;
                }
            }
            if (end - j >= 2) {
                score += table[i][j] * (end - j + 1);
                for (int k = j; k <= end; k++) {
                    table[i][k] = -k;
                }
                break;
            }
        }
    }
    return score;
}

void drop(Vec<Vec<int> >& table, int h) {
    while (1) {
        Vec<Vec<int> > new_table = table;
        for (int i = h - 1; i > 0; i--) {
            for (int j = 0; j < 5; j++) {
                if (new_table[i][j] == -j) {
                    new_table[i][j] = new_table[i - 1][j];
                    new_table[i - 1][j] = -j;
                }
            }
        }

        if (new_table == table) {
            break;
        } else {
            table = new_table;
        }
    }
    return;
}

int main(void) {
    while (1) {
        int h;
        cin >> h;
        if (h == 0) {
            break;
        }
        Vec<Vec<int> > table(h, Vec<int>(5));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < 5; j++) {
                cin >> table[i][j];
            }
        }
        int score = 0;
        while (1) {
            int this_score = del(table, h);
            if (this_score == 0) {
                break;
            } else {
                score += this_score;
                drop(table, h);
            }
        }
        cout << score << endl;
    }

    return 0;
}

void dump(Vec<Vec<int> >& table, int h) {
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < 5; j++) {
            cout << table[i][j] << " ";
        }
        cout << endl;
    }
    return;
}

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

const int H = 10, W = 5;
int board[H][W];

int main(){
	int n, s, inc, c;
	while(cin >> n, n){
		s = inc = 0;
		for(int i = 0;i < n;++i){
			for(int j = 0;j < W;++j){
				cin >> board[i][j];
			}
		}
		do {
			inc = 0;
			for(int i = 0;i < n;++i){
				for(int j = 0;j < W - 2;++j){
					c = 0;
					while(j + c < W && board[i][j + c] == board[i][j]){
						++c;
					}
					if(c >= 3){
						inc += c * board[i][j];
						for(int k = 0;k < c;++k){
							board[i][j + k] = 0;
						}
					}
				}
			}
			for(int i = 1;i < n;++i){
				for(int j = 0;j < W;++j){
					if(!board[i][j]){
						for(int k = i;k > 0;--k){
							board[k][j] = board[k - 1][j];
						}
					}
				}
			}
//			for(int i = 0;i < n;++i){
//				for(int j = 0;j < W;++j){
//					cout << board[i][j];
//				}
//				cout << endl;
//			}
//			cout << endl;
			s += inc;
		} while (inc);
		cout << s << endl;
	}
}

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

int h, d[10][5];

int check() {
  int counter = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < 3; j++) {
      if (d[i][j] == -10) continue;
      if (d[i][j] == d[i][j + 1] && d[i][j] == d[i][j + 2]) {
        int num = d[i][j];
        for (int k = j; k < 5; k++) {
          if (d[i][k] == num) d[i][k] = -1, counter += num;
          else break;
        }
        break;
      }
    }
  }
  return counter;
}

void fall() {
  for (int j = 0; j < 5; j++) {
    int counter = 0, temp = -1;
    for (int i = h - 1; i >= 0; i--) {
      if (d[i][j] == -10) { temp = i; break; }
      if (d[i][j] == -1) counter++;
      else d[i + counter][j] = d[i][j];
    }
    for (int i = 0; i < counter; i++) d[temp + 1 + i][j] = -10;
  }
}

int main() {
  while (1) {
    scanf("%d", &h);
    if (h == 0) break;
    for (int i = 0; i < h; i++) for (int j = 0; j < 5; j++) scanf("%d", &d[i][j]);
    int sum = 0;
    while (1) {
      int counter = check();
      if (counter == 0) break;
      sum += counter;
      fall();
    }
    printf("%d\n", sum);
  }
}