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 <vector>
#include <algorithm>

using namespace std;

int main()
{
    int h;
    while(1){

        
        cin >> h;
        if(h == 0){
            break;
        }

        
        vector<vector<int>> stone;
        stone.resize(5);
        for(int i = 0; i < 5; i++){
            stone[i].resize(h);
        }
        for(int i = h-1; i >= 0; i--){
            for(int j = 0; j < 5; j++){
                cin >> stone[j][i];
            }
        }
        
        int old_score = 0;
        int score = 0;
        while(1){
            old_score = score;
            for(int i = 0; i < h; i++){
                for(int j = 0; j < 3; j++){
                    if(stone[j][i] == stone[j+1][i] && stone[j][i] == stone[j+2][i] && stone[j][i] != 0){
                        int old_stone_num = stone[j][i];
                        for(int k = 0; j + k < 5 && stone[j+k][i] == old_stone_num; k++){
                            score += stone[j+k][i];
                            stone[j+k][i] = 0;
                        }
                        break;
                    }
                }
            }
            
            if(old_score == score){
                break;
            }
        
            
            for(int i = 0; i < 5; i++){
                remove(stone[i].begin(), stone[i].end(), 0), stone[i].end();
            }

        }
        cout << score << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <vector>

int disappear(std::vector<std::vector<int> > &p) {
  size_t W=p.size(), H=p[0].size();
  std::vector<std::vector<int> > to_erase(W, std::vector<int>(H));
  int diff=0;

  for (size_t i=0; i<H; ++i) {
    int l=2, r=3;
    if (p[2][i] == 0) continue;

    while (l > 0) {
      if (p[l][i] == p[l-1][i]) {
	--l;
      } else {
	break;
      }
    }

    while (r < 5) {
      if (p[r][i] == p[r-1][i]) {
	++r;
      } else {
	break;
      }
    }

    if (r-l < 3)
      continue;

    diff += (r-l)*p[2][i];
    for (int j=l; j<r; ++j)
      to_erase[j][i] = 1;
  }

  for (size_t j=0; j<W; ++j) {
    std::vector<int> tmp(H);
    size_t k=0;
    for (size_t i=H; i--;) {
      if (to_erase[j][i]) continue;
      if (p[j][i] == 0) break;

      tmp[k++] = p[j][i];
    }

    std::reverse(tmp.begin(), tmp.end());
    p[j] = std::move(tmp);
  }

  return diff;
}

int solve_testcase() {
  size_t H, W=5;
  scanf("%zu", &H);
  if (H == 0)
    return 1;

  int score=0;
  std::vector<std::vector<int> > board(W, std::vector<int>(H));  // irreg
  for (size_t i=0; i<H; ++i) {
    for (size_t j=0; j<W; ++j) {
      scanf("%d", &board[j][i]);
    }
  }

  int diff;
  while ((diff = disappear(board))) {
    score += diff;
  }

  printf("%d\n", score);
  return 0;
}

int main() {
  while (!solve_testcase()) {}

  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <cmath>

using namespace std; using namespace placeholders;

typedef vector< int > VI;
typedef vector< VI > VVI;

template < typename T > inline T fromString( const string &s ){ T res; istringstream iss( s ); iss >> res; return res; };
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( e, c ) for ( auto &e : c )
#define ALL( c ) (c).begin(), (c).end()
#define AALL( a, t ) (t*)a, (t*)a + sizeof( a ) / sizeof( t )
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

int vanish( VVI &board )
{
	int res = 0;

	FOR( row, board )
	{
		REP( i, 0, 5 )
		{
			for ( int j = 4; i + 2 <= j; j-- )
			{
				if ( row[i] && set<int>( row.begin() + i, row.begin() + j + 1 ).size() == 1 )
				{
					res += row[i] * ( j - i + 1 );
					fill( row.begin() + i, row.begin() + j + 1, 0 );
				}
			}
		}
	}

	return res;
}

VVI fall( const VVI &board )
{
	VVI res( board.size(), VI( 5 ) );

	REP( x, 0, 5 )
	{
		VI bricks;
		REP( y, 0, board.size() )
		{
			if ( board[y][x] )
			{
				bricks.PB( board[y][x] );
			}
		}
		reverse( ALL( bricks ) );
		REP( i, 0, bricks.size() )
		{
			res[ res.size() - 1 - i ][x] = bricks[i];
		}
	}

	return res;
}

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	for ( int h; cin >> h, h; )
	{
		VVI board( h, VI( 5 ) );
		FOR( row, board )
		{
			FOR( a, row )
			{
				cin >> a;
			}
		}

		int res = 0;
		for ( int d; d = vanish( board ); res += d, board = fall( board ) );
		cout << res << endl;
	}

	return 0;
}