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;
int main(){
  int H, h, b, e;
  while (cin >> H, H){
    int C[H][5], ans = 0;
    for (int i = 0; i < H; ++i){
      for (int j = 0; j < 5; ++j){
	cin >> C[H-1-i][j];
      }
    }
    bool f = true;
    vector< vector<int> > V;
    while (f){
      f = false;
      for (int i = 0; i < H; ++i){
	b = 0;
	e = 0;
	for (int j = 1; j < 5; ++j){
	  if (C[i][j] && C[i][j-1] == C[i][j]){
	    e = j;
	  }else{
	    if (e - b > 1){
	      f = true;
	      V.push_back({i,b,e});
	    }
	    b = j;
	    e = j;
	  }
	}
	if (e - b > 1){
	  f = true;
	  V.push_back({i,b,e});
	}
      }
      while (!V.empty()){
	h = V.back()[0];
	b = V.back()[1];
	e = V.back()[2];
	V.pop_back();
	ans += C[h][b]*(e-b+1);
	for (int i = h; i < H; ++i){
	  for (int j = b; j <= e; ++j){
	    if (i+1 < H){
	      C[i][j] = C[i+1][j];
	    }else{
	      C[i][j] = 0;
	    }
	  }
	}
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define REP(i,s,n) for(int i=s ; i < n ; i++)
#define rep(i,n) REP(i,0,n)
using namespace std;

int field[10][5];
int cnt;

void Connect_Num(int y,int x){
  
  cnt++;
  
  if( x+1 < 5 && field[y][x] == field[y][x+1]){
    Connect_Num(y,x+1);
  }
  
}
void Del(int y,int x,int n){
  
  rep(i,n){
    for(int j=y ; j > 0 ; j--){
      field[j][x+i]=field[j-1][x+i];
    }
    field[0][x+i]=0;
  }
  
}
int main(){
  
  int h;
  
  while(1){
    
    cin >>h;
    if(h==0)break;
    
    rep(i,h){
      rep(j,5){
	cin >>field[i][j];
      }
    }
    
    int score=0;
    
    while(1){
      bool flag=true;
      rep(i,h){
	rep(j,5){
	  if(field[i][j] != 0){
	    cnt=0;
	    Connect_Num(i,j);
	    if(cnt >=3){
	      score+=cnt*field[i][j];
	      Del(i,j,cnt);
	      j+=cnt;
	      flag=false;
	    }
	  }
	}
      }
      if(flag)break;
    }
    
    cout <<score<<endl;
  
  }
  
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;


int h,g[11][11];

bool fall(int x,int y){

  if(g[y][x]==0)return false;

  bool fg=false;
  int tmp=g[y][x];
  g[y][x]=0;
  while(y>0 && g[y-1][x]==0)y--,fg=true;
  g[y][x]=tmp;
  return fg;
}

int remove(int x,int y){

  if(g[y][x]==0)return 0;
  
  int res=g[y][x],cnt=0;
  while(g[y][x]==g[y][x+cnt])cnt++;

  if(cnt<3)return 0;
    
  for(int i=0;i<cnt;i++)g[y][x+i]=0;
  
  return res*cnt;
}

void print(){
    for(int i=h-1;i>=0;i--){
      for(int j=0;j<5;j++)if(g[i][j]<0)cout << "*";else cout << g[i][j];
      cout << endl;
    }
}

int solve(){

  bool fg;
  int res=0;

  while(true){
    fg=false;
    
    for(int i=0;i<h;i++){
      for(int j=0;j<5;j++){
	res+=remove(j,i);
      }
    }

    for(int i=0;i<h;i++){
      for(int j=0;j<5;j++){
	fg|=fall(j,i);
      }
    }

    if(!fg)return res;
  }
}

int main(void){
  
  while(cin >> h,h){

    for(int i=0;i<11;i++)
      for(int j=0;j<11;j++)g[i][j]=0;

    for(int i=h-1;i>=0;i--){
      for(int j=0;j<5;j++)cin >> g[i][j];
    }

    cout << solve() << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <iomanip>
#include <deque>
#include <stdio.h>
using namespace std;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define PB_VEC(Itr1,Itr2) (Itr1).insert((Itr1).end(),(Itr2).begin(),(Itr2).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())
#define LBOUND(Itr,val) lower_bound((Itr).begin(),(Itr).end(),(val))
#define UBOUND(Itr,val) upper_bound((Itr).begin(),(Itr).end(),(val))
typedef long long ll;

int cnt(vector< vector<int> > a){
    int res=0;
    while(true){
        
        bool flag=false;
        REP(i,a.size()){
            int c=a[i][0];
            int cnt=0;
            int maxcnt=0;
            int maxc=-1;
            int g=-1;
            REP(j,5){
                if(c!=a[i][j]){
                    cnt=1;
                    c=a[i][j];
                }else{
                    cnt++;
                }
                if(maxcnt<cnt){
                    maxcnt=cnt;
                    maxc=c;
                    g=j;
                }
            }
            
            if(maxcnt>=3 && maxc!=0){
                flag=true;
                for(int j=g-maxcnt+1;j<5;j++){
                    if(a[i][j]==maxc)a[i][j]=0;
                }
                res+=maxcnt*maxc;
            }
        }
    
        if(!flag)break;
      
        for(int i=(int)a.size()-1;i>=0;i--){
            REP(j,5){
                if(a[i][j]==0){
                    for(int k=i-1;k>=0;k--){
                        a[k+1][j]=a[k][j];
                        if(k==0)a[k][j]=0;
                    }
                }
            }
        }
    }
    return res;
}

int main(){
    
    while(true){
        int n; cin>>n;
        if(n==0)break;
        vector< vector<int> > a(n,vector<int>(5));
        REP(i,n)REP(j,5)cin>>a[i][j];
        cout<<cnt(a)<<endl;
    }
    
    return 0;
}