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 <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <tuple>
#include <cassert>
#include <exception>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i,a,n) for(ll i = (a);i < (n);i++)
#define per(i,a,n) for(ll i = (a);i > (n);i--)
#define lep(i,a,n) for(ll i = (a);i <= (n);i++)
#define pel(i,a,n) for(ll i = (a);i >= (n);i--)
#define clr(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define sz size()
#define print(X) cout << (X) << endl
static const int INF = 1e+9+7;
ll n,m,l;
string s,t;
int d[200010],dp[1010][1010];
double w[1000],v[1000];
double box[200010];
int field[200][200];

int main(){
  while(1){
    cin >> n;
    if(!n)break;
    clr(field,0);
    rep(i,0,n)rep(j,0,5)cin >> field[i][j];
    ll ans = 0;
    bool f = true;
    while(f){
      f = false;
      rep(i,0,n){
        rep(j,0,5){
          l = 1;
          rep(k,j,5){
            if(field[i][k] == 0)break;
            if(field[i][k] != field[i][k+1])break;
            l++;
          }
          if(l >= 3){
            f = true;
            rep(k,j,j+l){
              ans += field[i][k];
              field[i][k] = 0;
            }
          }
        }
      }
      // rep(i,0,n){
      //   rep(j,0,5)cout << field[i][j];
      //   puts("");
      // }
      // puts("");
      pel(i,n-1,0){
        rep(j,0,5){
          if(!field[i][j]){
            // cout << i << " " << j << endl;
            l = 0;
            pel(k,i,0){
              // if(k + i >= n)break;
              if(!field[k][j])l++;
              else break;
            }
            // if(l + i >= n)break;
            rep(k,0,n-i){
              field[i-k][j] = field[i-k-l][j];
              field[i-k-l][j] = 0;
            }
          }
        }
      }
      // rep(i,0,n){
      //   rep(j,0,5)cout << field[i][j];
      //   puts("");
      // }
      // puts("===================================");
    }
    print(ans);
  }
  return 0;
}

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

int main()
{
  
  while(1){
    int h;
    cin >> h;
    if(h == 0) break;
    int s[h+1][5];
    fill(&s[0][0], &s[0][5], -1);
    for(int i = 1; i < h+1; i++){
      for(int j = 0; j < 5; j++){
        cin >> s[i][j];
      }
    }
    int ans = 0;
    int c = 0;
    while(1){
      for(int i = h; i > 0; i--){
        if(s[i][0] == s[i][1] && s[i][1] == s[i][2] && s[i][2] == s[i][3] && s[i][3] == s[i][4] && s[i][0] > 0){
          ans += s[i][0]*5;
          fill(&s[i][0], &s[i][5], 0);
        }else if(s[i][0] == s[i][1] && s[i][1] == s[i][2] && s[i][2] == s[i][3] && s[i][0] > 0){
          ans += s[i][0]*4;
          fill(&s[i][0], &s[i][4], 0);
        }else if(s[i][1] == s[i][2] && s[i][2] == s[i][3] && s[i][3] == s[i][4] && s[i][1] > 0){
          ans += s[i][1]*4;
          fill(&s[i][1], &s[i][5], 0);
        }else if(s[i][0] == s[i][1] && s[i][1] == s[i][2] && s[i][0] > 0){
          ans += s[i][0]*3;
          fill(&s[i][0], &s[i][3], 0);
        }else if(s[i][2] == s[i][3] && s[i][3] == s[i][4] && s[i][2] > 0){
          ans += s[i][2]*3;
          fill(&s[i][2], &s[i][5], 0);
        }else if(s[i][1] == s[i][2] && s[i][2] == s[i][3] && s[i][1] > 0){
          ans += s[i][1]*3;
          fill(&s[i][1], &s[i][4], 0);
        }else{
          c++;
        }
      }
      if(c == h) break;
      c = 0;
      for(int i = 0; i < 5; i++){
        for(int j = h; j > 0; j--){
          if(s[j][i] == -1){
            for(int k = j; k >= 0; k--){
              s[k][i] = -1;
            }
            break;
          }
          while(s[j][i] == 0){
            for(int k = j; k > 0; k--){
              swap(s[k][i], s[k-1][i]);
            }
            s[0][i] = -1;
          }
        }
      }
    }
    cout << ans << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define reps(i,j,k) for(int i=j;i<k;++i)
#define rep(i,j) reps(i,0,j)
int stage[12][12];
int W,H;
int check(){
	int prev = -1;
	int cnt = 0;
	int res = 0;
	rep(i,H){
		rep(j,W){
			if(prev != stage[i][j] || prev == 0){
				if(cnt >= 3){
					res += stage[i][j-1]*cnt;
					reps(k,j-cnt,j){
						stage[i][k] = 0;
					}
				}
				prev = stage[i][j];
				cnt = 1;
			}
			else{
				cnt++;
			}
		}
		if(cnt >= 3){
			res += stage[i][W-1]*cnt;
			reps(k,W-cnt,W){
				stage[i][k] = 0;
			}
		}
		cnt = 0;
		prev = -1;
	}
	return res;
}

void drop(){
	rep(i,H-1){
		rep(j,W){
			int c = 0;
			while(stage[i][j] == 0){
				if(c == H)break;
				reps(k,i+1,H){
					stage[k-1][j] = stage[k][j];
				}
				c++;
			}
		}
	}
}

int main(){
	W = 5;
	while(scanf("%d",&H),H){
		rep(i,12){
			rep(j,12){
				stage[i][j] = 0;
			}
		}
		rep(i,H){
			rep(j,W){
				scanf("%d",&stage[H-i-1][j]);
			}
		}

		int score = 0;
		while(true){
			int tmp = check();
			if(tmp == 0)break;
			score += tmp;
			drop();
		}	
		printf("%d\n",score);
	}
	return 0;
}