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 <algorithm>
#include <iostream>
#include <vector>
using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;

int H;
int W;
vvi g;

int erase(vvi &g) {
    int res = 0;
    for (size_t j = 0; j < W; j++) {
        for (size_t i = 0; i < H; i++) {
            if (g[i][j] == 0 || g[i][j] == -1) continue;
            int k = i + 1;
            while (k < H && g[i][j] == g[k][j]) {
                ++k;
            }
            int d = k - i;
            if (d >= 3) {
                for (int l = i; l < k; ++l) {
                    res += g[l][j];
                    g[l][j] = -1;
                }
            }
        }
    }
    for (size_t i = 0; i < H; i++) {
        auto &row = g[i];
        auto it = remove(row.begin(), row.end(), -1);
        row.erase(it, row.end());
        while (row.size() < W) {
            row.push_back(0);
        }
    }
    return res;
}

int main() {
    while (cin >> H && H) {
        W = 5;
        g.assign(W, vi(H));
        for (size_t i = 0; i < H; i++) {
            for (size_t j = 0; j < W; j++) {
                cin >> g[j][i];
            }
        }
        swap(H, W);
        for (size_t i = 0; i < H; i++) {
            reverse(g[i].begin(), g[i].end());
        }
        int ans = 0;
        while (1) {
            int add = erase(g);

            //for (size_t i = 0; i < H; i++) {
            //    for (size_t j = 0; j < W; j++) {
            //        cout << g[i][j];
            //    }
            //    cout << endl;
            //}
            //cout << endl;


            if (add == 0) break;
            ans += add;
        }
        cout << ans << endl;
    }
}

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

using namespace std;

int main()
{
    while (true) {
        int h;
        scanf("%d", &h);
        if (h == 0) {
            break;
        }

        vector<vector<int>> v;
        v.resize(h);
        for (auto&& r : v) {
            r.resize(5);
            for (auto i = 0; i < 5; ++i) {
                int x;
                scanf("%d", &x);
                r[i] = x;
            }
        }

        int p = 0;
        bool change = false;
        do {
            change = false;
            for (auto&& r : v) {
                // 5
                if (r[0] > 0 && r[0] == r[1] && r[1] == r[2] && r[2] == r[3] &&
                    r[3] == r[4]) {
                    p += r[0] * 5;
                    r[0] = r[1] = r[2] = r[3] = r[4] = -1;
                    change = true;
                }
                // 4
                if (r[0] > 0 && r[0] == r[1] && r[1] == r[2] && r[2] == r[3]) {
                    p += r[0] * 4;
                    r[0] = r[1] = r[2] = r[3] = -1;
                    change = true;
                }
                if (r[1] > 0 && r[1] == r[2] && r[2] == r[3] && r[3] == r[4]) {
                    p += r[1] * 4;
                    r[1] = r[2] = r[3] = r[4] = -1;
                    change = true;
                }
                // 3
                if (r[0] > 0 && r[0] == r[1] && r[1] == r[2]) {
                    p += r[0] * 3;
                    r[0] = r[1] = r[2] = -1;
                    change = true;
                }
                if (r[1] > 0 && r[1] == r[2] && r[2] == r[3]) {
                    p += r[1] * 3;
                    r[1] = r[2] = r[3] = -1;
                    change = true;
                }
                if (r[2] > 0 && r[2] == r[3] && r[3] == r[4]) {
                    p += r[2] * 3;
                    r[2] = r[3] = r[4] = -1;
                    change = true;
                }
            }

            for (int i = 0; i < 5; ++i) {
                vector<int> t;
                // ???????????£??????????????¢???
                for (size_t j = 0; j < v.size(); ++j) {
                    if (v[j][i] > 0) {
                        t.push_back(v[j][i]);
                    }
                }
                // ?????£????????????????????°??????????????????
                for (size_t j = 0; j < t.size(); ++j) {
                    v[h - t.size() + j][i] = t[j];
                }
                // ?????£?????¨??????????¶????????????¨?????????
                for (size_t j = 0; j < h - t.size(); ++j) {
                    v[j][i] = -1;
                }
            }
        } while (change);
        printf("%d\n", p);
    }
}

C++: #include <iostream>
using namespace std;
int main(){
  int h;
  while (cin>>h,h|h!=0){
    string temp;
    int board[h][6];
    for (int i=0;i<h;i++){
      cin>>board[i][0]>>board[i][1]>>board[i][2]>>board[i][3]>>board[i][4];
    }
    int num,count;
    int check=0;
    int score=0;
    while (1){
      check=score;
      for (int i=h-2;i>=0;i--){
	for (int j=0;j<5;j++){
	  for (int k=h-1;k>i;k--){
	    if (board[k][j]==0){
	      board[k][j]=board[i][j];
	      board[i][j]=0;
	      break;
	    }
	  }
	}
      }
      for (int i=0;i<h;i++){
	count=1;
	num=0;
	for (int j=0;j<5;j++){
	  if (board[i][j]==num){
	    count++;
	  }
	  else{
	    if (count>2)
	      break;
	    count=1;
	  }
	  num=board[i][j];
	}
	if (count<=2)
	  num=0;
	score=score+num*count;
	for (int j=0;j<5;j++){
	  if (board[i][j]==num&&count>1){
	    board[i][j]=0;
	    count--;
	  }
	}
      }
      if (check==score){
	break;
      }

    }

    cout<<score<<endl; 
  }
}