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>
using namespace std;

class game{
private:
  vector<vector<int> > map;
  int score;
  int size;
public:
  game(int s){
    size=s;
    map.resize(size);
    for(int i=0;i<size;i++){
      map[i].resize(5);
    }
    score=0;
  }

  void setnum(int num,int i,int j){
    map[i][j]=num;
  }

  void deleteline(){
    for(int i=0;i<size;i++){
      int c=1;
      int index = 0;
      int end = 0;
      int delnum=0;
      for(int j=0;j<4;j++){
	if(map[i][j]==map[i][j+1]) c++;
	else c=1;
	if(c>=3){
	  delnum=map[i][j];
	  index = j - c + 1;
	  end = j;
	}
      }
      for(int j=index;j<=end+1;j++){
	if(map[i][j]==delnum){map[i][j]=0;score+=delnum;}
      }
    }
  }

  void falldown(){
    for(int c=0;c<=size;c++){
      for(int i=size-2;i>=0;i--){
	for(int j=0;j<5;j++){
	  if(map[i+1][j]==0){ map[i+1][j]=map[i][j]; map[i][j]=0;} 
	}
      }
    }
  }

  int calc(){
    int bf = -1;
    while(bf != score){
      bf = score;
      deleteline();
      falldown();
      //        show();
    }
    return score;
  }
  /*
  void show(){
    cout<<endl;
    for(int i=0;i<size;i++){
      for(int j=0;j<5;j++){
	cout<<map[i][j]<<" ";
      }
      cout<<endl;
    }
    cout<<endl;
    }*/
  

};

int main(){
  int t=1;
  while(cin>>t){
    if(t == 0) break;
    game g(t);
    for(int i=0;i<t;i++){
      for(int j=0;j<5;j++){
	int num;
	cin>>num;
	g.setnum(num,i,j);
      }
    }
    cout<<g.calc()<<endl;

  }
  return 0;
}

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

int erase(vector<vector<int>>& field) {
    int H = field.size(), W = 5;
    int score = 0;

    for(int i = 0; i < H; ++i) {
        int target = 0, right = 0;
        int count = 0;
        for(int j = 1; j < W; ++j) {
            if(field[i][j] == field[i][j - 1]) ++count;
            else count = 0;
            if(count > 1) {
                target = field[i][j];
                right = j;
            }
        }

        for(int j = right; j >= 0; --j) {
            if(field[i][j] != target) break;
            score += target;
            field[i][j] = 0;
        }
    }

    return score;
}

void fall(vector<vector<int>>& field) {
    int H = field.size(), W = 5;
    for(int j = 0; j < W; ++j) {
        for(int i = H - 1; i > 0; --i) {
            if(field[i][j] != 0) continue;
            for(int k = i - 1; k >= 0; --k) {
                if(field[k][j] == 0) continue;
                swap(field[i][j], field[k][j]);
                break;
            }
        }
    }
}

int main() {
    int H, W = 5;
    while(cin >> H, H) {
        vector<vector<int>> field(H, vector<int>(W));
        for(int i = 0; i < H; ++i) for(int j = 0; j < W; ++j) cin >> field[i][j];

        int score = 0;
        while(int add = erase(field)) {
            score += add;
            fall(field);
        }

        cout << score << endl;
    }
}

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

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;


int main() {
    while(1){
        int H; cin >> H;

        if(H==0){
            return 0;
        }
        vector<vector<int>> board(H+1, vector<int>(5));
        FOR(i,1,H+1){
            REP(j,5){
                cin >> board[i][j];
            }
        }

        int ans = 0;
        int ans_before = 0;
        while(1){
            FOR(i,1,H+1){
                int num = 0;
                int count = 0;
                REP(j,5){
                    if(num == board[i][j]){
                        count++;
                        if(count >= 3 && j==4){
                            ans += count * num;
                            for(int k= j; k>=j-count+1; k--){
                                board[i][k] = 0;
                            }
                        }
                    }else{
                        if(count >= 3){
                            ans += count * num;
                            for(int k= j-1; k>=j-count; k--){
                                board[i][k] = 0;
                            }
                        }
                        num = board[i][j];
                        count = 1;
                    }
                }  
            }
            REP(n,H){
                for(int i=H; i>=1; i--){
                    REP(j,5){
                        if(board[i][j]==0){
                            board[i][j]=board[i-1][j];
                            board[i-1][j]=0;
                        }
                    }
                }
            }

            if(ans == ans_before){
                cout << ans << endl;
                break;
            }
            ans_before = ans;

            // FOR(i,1,H+1){
            //     REP(j,5){
            //         cout << board[i][j] << " ";
            //     }
            //     cout << endl;
            // }

        }

    }
    return 0;
}