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;

#define endl "\n"
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define rrep(i, a, b) for (auto i = (a); i > (b); --i)

void solve(int &n)
{
  int a[10][5];
  rep(i, 0, 10) rep(j, 0, 5) a[i][j] = -1;
  rrep(i, n - 1, -1)
  {
    rep(j, 0, 5)
    {
      cin >> a[i][j];
    }
  }
  bool change = true;
  int ans = 0;
  while (change)
  {
    change = false;
    int b[10][5];
    rep(i, 0, 10) rep(j, 0, 5) b[i][j] = -1;
    rep(i, 0, 10)
    {
      rrep(len, 5, 2)
      {
        rep(start, 0, 5 - len + 1)
        {
          bool ok = true;
          int now = a[i][start];
          rep(j, start, start + len)
          {
            if (now != a[i][j] || now == -1)
            {
              ok = false;
            }
          }
          if (ok)
          {
            change = true;
            rep(j, start, start + len)
            {
              ans += a[i][j];
              a[i][j] = -1;
            }
          }
        }
      }
    }
    rep(j, 0, 5)
    {
      int go = 0;
      rep(i, 0, 10)
      {
        if (a[i][j] != -1)
        {
          b[go][j] = a[i][j];
          go++;
        }
      }
    }
    rep(i, 0, 10) rep(j, 0, 5) a[i][j] = b[i][j];
  }
  cout << ans << endl;
}

int main()
{
  int n;
  while (cin >> n, n)
  {
    solve(n);
  }
}

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

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for(int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)
#define BIT(x, i) (((x) >> (i)) & 1)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vc> vvc;
typedef vector<vll> vvll;

int main() {
  int h, w = 5;
  while(cin >> h, h) {
    vvi field(h + 1, vi(w));
    REP(y, h) REP(x, w){
      cin >> field[y][x];
    }
    REP(x, w){
      field[h][x] = x + 1;
    }

    int res = 0;
    bool update = true;
    while(update) {
      update = false;
      // erase
      REP(y, h) {
        auto &ln = field[y];
        auto c = ln.begin() + 2;
        auto l = c;
        auto r = c;
        while(l != ln.begin() && *(l - 1) == *c) l--;
        while(r != ln.end() && *(r + 1) == *c)  r++;
        if (++r - l >= 3) {
          res += (r - l) * *c;
          fill(l, r, 0);
        }
      }

      // drop
      REP(x, w) {
        RREP(y, h - 1) {
          if (!field[y + 1][x] && field[y][x]) {
            swap(field[y + 1][x], field[y][x]);
            update = true;
            y += 2;
          }
        }
      }
    }

    cout << res << endl;

  }
}

C++: #include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <stack>
#include <cmath>
using namespace std;

int H, score;
int board[10][5];

bool checkBoard(){
  bool flag = false;
  for(int i = 0; i < H; i++){
    int prev = board[i][0], head = 0, tail = 1;
    for(int j = 1; j < 5; j++){
      if(board[i][j] == prev && prev != 0) tail++;
      else{
        prev = board[i][j];
        if(tail - head >= 3) continue;
        head = j;
        tail = head + 1;
      }
    }

    if(tail - head < 3) continue;
    score += board[i][head] * (tail - head);
    flag = true;

    for(int j = i; j >= 1; j--){
      for(int s = head; s < tail; s++) board[j][s] = board[j-1][s];
    }

    for(int j = head; j < tail; j++) board[0][j] = 0;
  }

  return flag;
}

int main(void)
{
  while(cin >> H && H != 0){
    score = 0;
    for(int i = 0; i < H; i++){
      for(int j = 0; j < 5; j++) cin >> board[i][j];
    }
    while(checkBoard());
    cout << score << endl;
  }
  return 0;
}

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

#define rep(i,n) for(int i=0;i<(n);i++)

int main()
{
	for(int h;cin>>h && h;){
		vector<vector<int>> a(h,vector<int>(5));
		rep(i,h) rep(j,5) cin>>a[i][j];
		int res=0;
		for(;;){
			int add=0;
			for(auto& b:a) for(int j=0;j<5;){
				if(b[j]==0){
					j++;
					continue;
				}
				int k=j;
				while(k<5 && b[k]==b[j]) k++;
				if(k-j>=3){
					add+=(k-j)*b[j];
					fill(begin(b)+j,begin(b)+k,0);
				}
				j=k;
			}
			if(add==0) break;
			res+=add;
			rep(i,5) for(int j=h,k=h;k--;)
				if(a[k][i]) swap(a[--j][i],a[k][i]);
		}
		cout<<res<<endl;
	}
}