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;

int main(){
	int N, T, t[10010] = {}, a = 1, l, r;
	cin>>N>>T;
	for(int i = 1; i <= N; i++){
		cin>>l>>r;
		for(int j = l + 1; j <= r; j++){
			t[j]++;
		}
	}
	for(int i = 1; i <= N; i++){
		if(t[i] >= t[a]) a = i; 
	}
	
	cout<<t[a]<<endl;
	return 0;
}

C++: 
#include "bits/stdc++.h"

#define sai(j,n) for(sei j=0;j<sei(n);++j)
#define hsai(j,n) for(sei j=sei(n)-1;j>=0;--j)
#define kan(j,m,n) for(sei j=m;j<sei(n);++j)
#define hkan(j,m,n) for(sei j=sei(n)-1;j>=sei(m);--j)
#define zen(v) (v).begin(),(v).end()
#define yu(v) v.erase(unique(zen(v)), v.end());
#define syutu(v) si(a, (v).size()) {cout << v[a];if(a != v.size()-1)cout << " ";else cout << endl;}
#define mug 10000000111;
#define yo 10000000711;
#define kyo 1e-9;

const int dx[8] = {1,1,0,-1,-1,-1,0,1};
const int dy[8] = {0,1,1,1,0,-1,-1,-1};

using namespace std;
typedef long long sei;
typedef long double syo;
typedef vector<sei> vs;
typedef vector<syo> vy;
typedef vector<vs> vvs;
typedef vector<vy> vvy;
typedef pair<sei, sei> kS;
sei max(sei a, int b) {return max(a, sei(b));}
sei max(int a, sei b) {return max(sei(a), b);}

sei kaiki(sei n){
    static sei ki[1001];

    if (n==0) return 0;
    if (n==1) return 1;

    if(ki[n] ==0) {
        ki[n] = kaiki(n-1) + n;
    }else{
    }
    return ki[n];
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    sei a, b, c, d;
    cin >> a  >> b;
    c = b - a;
    cout << kaiki(c)-b << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
#define REP(a,b,c) for(int a=b; a<(int)c; a++)
#define REPD(a,b,c) for(int a=b; a>(int)c; a--)
#define rep(a,b) REP(a,0,b)
#define repd(a,b) REPD(a,b,0)
typedef long long int ll;
using namespace std;

void show(vector<vector<int> > board){
  rep(i,board.size()){
    rep(j,5){
      cout << board[i][j];
    }
    cout << endl;
  }
}


int main(){
  while(true){
    int H;
    cin >> H;
    if(H == 0) break;
    vector<vector<int> > board(H, vector<int>(5, 0));
    rep(i,H)rep(j,5) cin >> board[i][j];
    int ans = 0;
    while(true){
      bool isFin = true;
      //      show(board);
      //cout << endl;
      rep(i,H){
        int num = board[i][0], cnt = 1;
        int j;
        for(j=1;j<5; j++){
          if(num == board[i][j]){
            cnt++;
          }else{
            if(cnt >= 3) break;
            num = board[i][j];
            cnt = 1;
          }
        }
        if(cnt >= 3 && num != 0){
          ans += cnt * num;
          int first_idx = j-cnt;
          for(int k=0; k < cnt; k++){
            board[i][first_idx+k] = 0;
          }
          isFin = false;
        }
      }
      for(int i=H-1; i>=0; i--){
        rep(j,5){
          if(board[i][j] == 0){
            for(int k=i-1; k>=0; k--){
              if(board[k][j] != 0){
                board[i][j] = board[k][j];
                board[k][j] = 0;
                break;
              }
            }
          }
        }
      }
      if(isFin) break;
    }
    cout << ans << endl;
  }
  return 0;
}

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

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
int h, sum;
vvi board;

void drop(int i, int j) {
	if (i == h - 1) return;
	if (board[i][j] > 0 && board[i + 1][j] == 0) {
		swap(board[i][j], board[i + 1][j]);
		drop(i + 1, j);
	}
}

int chain(int i, int j) {
	if (j == 4) return 1;
	if (board[i][j] != board[i][j + 1]) return 1;
	return 1 + chain(i, j + 1);
}

void calc() {
	int presum = sum;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < 5;) {
			int c = chain(i, j);
			if (c >= 3) {
				sum += board[i][j] * c;
				for (int k = 0; k < c; k++) {
					board[i][j + k] = 0;
				}
			}
			j += c;
		}
	}

	for (int i = h - 1; i >=0; i--) {
		for (int j = 0; j < 5; j++) {
			drop(i, j);
		}
	}
	if (presum != sum)
		calc();
}

int main() {
	while (cin >> h , h != 0) {
		board = vvi(h, vi(5, 0));
		sum = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < 5; j++) {
				cin >> board[i][j];
			}
		}
		calc();
		cout << sum << endl;
	}

}

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;
}