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;
using ll = long long;

template <typename TYPE>
void print_vec(const vector<TYPE>& v, int num=0){
  if(num == 0) num = (int)v.size();
	for(int i=0; i<num; i++){
		cout << v[i] << " ";
	}
	cout << endl;
}

template <typename TYPE>
void print_vec2(const vector<vector<TYPE>>& v){
  cout << endl; cout << "       ";
  for(int i=0; i<v[0].size(); i++) cout << i << "   "; 
  cout << endl;
	for(int i=0; i<v.size(); i++){
		cout << "i=" << i << ":   ";
		for(int j=0; j<v[i].size(); j++){
			if(v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << "   ";
      else cout << "\x1B[31m" << v[i][j] << "   ";//https://stackoverrun.com/ja/q/12618775
		}
		cout << "\x1B[0m" << endl;
	}
}

void calc(int H, int W){
  vector<vector<int>> maze(H, vector<int>(W));
  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      cin >> maze[i][j];
    }
  }

  int score = 0;
  int old_score = -1;
  while(score != old_score){
    old_score = score;
    for(int i=0; i<H; i++){

      map<int, int> mp;//カウント
      for(int j=0; j<W; j++) mp[maze[i][j]]++;

      //消滅処理
      for(pair<int, int> p: mp){
        // cout << " p.first: " << p.first << " p.second: " << p.second << endl;
        if(p.second >= 3){
          int cnt = 0;
          for(int j=0; j<W; j++){
            if(maze[i][j] == p.first) cnt++;
            else cnt = 0;

            if(cnt >= 3){
              for(int k=0; k<cnt; k++){
                if(maze[i][j-k] == p.first){
                  score += p.first;
                  maze[i][j-k] = 0;//消滅
                }
                else break;
              }
            }
          }
        }
      }
    }

    //ここまでで消滅と点数計算が終了
    //以下　落下の処理
    for(int i=H-1; i>=0; i--){
      for(int j=0; j<W; j++){
        if(maze[i][j] == 0){
          for(int k=i-1; k>=0; k--){
            if(maze[k][j] != 0){
              maze[i][j] = maze[k][j];
              maze[k][j] = 0;
              break;
            }
          }
        }
      }
    }
  }
  // cout << " maze: "; print_vec2(maze);
  cout << score << endl;
  return;
}

int main(){
  while(true){
    int H;
    cin >> H;
    if(H==0) return 0;
    const int W = 5;
    calc(H, W);
  }
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>

#define CH(N,A,B) (A<=N&&N<B)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define RREP(i,a,b) for(int i=(b-1);a<=i;i--)
#define F first
#define S second

using namespace std;

int H, stage[15][10];

int main() {
  while(1){
    cin>>H;
    if(H==0) break;
    REP(i,0,H){
      REP(j,0,5){
        cin>>stage[i][j];
      }
    }

    long long ans = 0;
    while(1){
      //fall
      REP(j,0,5){
        int botom = -1;
        for(int i=H-1; 0<=i; i--){
          if(stage[i][j]==-1){
            botom = i;
            break;
          }
        }
        int pos=botom-1;
        while(0<=pos && 0<botom){
          if(stage[pos][j]!=-1){
            swap(stage[pos][j],stage[botom][j]);
            botom--;
          }
          pos--;
        }
      }

      //delete
      bool flag = true;
      REP(i,0,H){
        int cnt = 1;
        REP(j,1,5){
          if(stage[i][j-1] == stage[i][j]){
            cnt++;
          }else{
            if(cnt>=3 && stage[i][j-1]!=-1){
              REP(k,j-cnt,j){
                ans += stage[i][k];
                stage[i][k] = -1;
                flag = false;
              }
            }
            cnt = 1;
          }
        }
        if(cnt>=3 && stage[i][5-1]!=-1){
          REP(k,5-cnt,5){
            ans += stage[i][k];
            stage[i][k] = -1;
            flag = false;
          }
        }
      }
      if(flag) break;
    }
    cout<<ans<<endl;
  }
  return 0;
}

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

using namespace std;

int main(){
	while(1){
		int h;
		cin >> h;
		if(h == 0) break;
		
		int puzle[10][5];
		int reffer [10][5];
		memset(puzle,0,sizeof(puzle));
		
		for(int i=0;i < h;i++){
			for(int j = 0;j < 5;j++){
				cin >> puzle[i][j];
				reffer[i][j] = i;
			}
		}
		
		int point = 0;
		while(true){
			int lastpoint = point;
			for(int i=0;i < h;i++){
				for(int j = 0;j < 5;j++){
					if(reffer[i][j] == -1) continue;
					int k;
					int left = puzle[reffer[i][j]][j];
					for(k = 1;j+k < 5 && left == puzle[reffer[i][j+k]][j+k];k++){
						
					}
					if(k >= 3){
						cout << i << ' ' << j << ' ' << k << endl;
						point += k*left;
						for(int p = i;p >= 0;p--){
							for(int q = j;q < j+k;q++){
								if(p == 0){
									reffer[p][q] = -1;
								}
								else{
									reffer[p][q] = reffer[p-1][q];
								}
							}
						}
						j += k;
					}
				}
			}
			if(lastpoint == point) break;
		}
		cout << point << endl;
	}
	return 0;
}