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;
typedef long long ll;
 
#define rep(i,n) for(int i=0; i<(n); ++i)

int stones[11][5];
int main(void){
    int H;
    while(cin >> H && H) {
        rep(i, H) rep(j, 5) cin >> stones[H-i-1][j];
        bool isFinished = false;
        int score = 0;
        while(!isFinished) {
            isFinished = true;
            rep(i, H) {
                rep(j, 3) {
                    if(stones[i][j]!=-1 &&
                       stones[i][j]==stones[i][j+1] &&
                       stones[i][j]==stones[i][j+2]) {
                        isFinished = false;
                        int d = stones[i][j];
                        for(; stones[i][j]==d && j<5; ++j) {
                            stones[i][j] = -1;
                            score += d;
                        }
                    }
                }
            }
            rep(column, 5) {
                for(int row=1; row<H; ++row) {
                    if(stones[row-1][column]==-1 && stones[row][column]!=-1) {
                        for(int i=row; i>0&&stones[i-1][column]==-1; --i) {
                            swap(stones[i-1][column], stones[i][column]);
                        }
                    }
                }
            }
        }
        cout << score << endl;
    }
}

C++: #include <cstdio>

#define int long long
#define dotimes(i, n) for (int i : range(n))

using namespace std;

class range {
  class iterator {
    int i, s;
  public:
    iterator(int i, int s): i(i), s(s) {}
    int& operator*() {
      return i;
    }
    void operator++() {
      i += s;
    }
    bool operator!=(iterator const& that) {
      return i < that.i;
    }
  };
  iterator b, e;
public:
  range(int e): b({0, 1}), e({e, 1}) {}
  range(int b, int e): b({b, 1}), e({e, 1}) {}
  range(int b, int e, int s): b({b, s}), e({e, s}) {}
  iterator begin() {
    return b;
  }
  iterator end() {
    return e;
  }
};

int rint() {
  int n;
  scanf("%lld", &n);
  return n;
}

void wint(int n) {
  printf("%lld\n", n);
}

template<typename T>
inline int size(T container) {
  return static_cast<int>(container.size());
}

template<typename T>
inline bool maxs(T& a, T const& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template<typename T>
inline bool mins(T& a, T const& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  for (;;) {
    int H = rint();
    if (!H)
      break;
    int board[H][6] = {};
    dotimes(i, H)
      dotimes(j, 5)
        board[H-i-1][j] = rint();
    int r = 0;
    for (;;) {
      int t = r;
      dotimes(i, H) {
        int s = board[i][0], c = 1;
        dotimes(j, 5) {
          if (board[i][j+1] == s)
            c++;
          else {
            if (s && c >= 3) {
              r += c * s;
              dotimes(k, c)
                board[i][j-k] = 0;
            }
            s = board[i][j+1];
            c = 1;
          }
        }
      }
      if (r == t)
        break;
      dotimes(j, 5) {
        int k = 0;
        dotimes(i, H)
          if (board[i][j])
            board[k++][j] = board[i][j];
        while (k < H)
          board[k++][j] = 0;
      }
    }
    wint(r);
  }
  return 0;
}

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

#define rep(i,n) for(int i=0;i<(n);i++)

int main()
{
	for(int h;cin>>h && h;){
		int a[h][5],res=0;
		rep(i,h) rep(j,5) cin>>a[i][j];
		for(;;){
			int add=0;
			for(auto& b:a) for(int j=0;j<5;){
				if(b[j]==0){
					j++;
					continue;
				}
				int k=j;
				while(k<5 && b[k]==b[j]) k++;
				if(k-j>=3){
					add+=(k-j)*b[j];
					fill(b+j,b+k,0);
				}
				j=k;
			}
			if(add==0) break;
			res+=add;
			rep(i,5) for(int j=h,k=h;k--;)
				if(a[k][i]) swap(a[--j][i],a[k][i]);
		}
		cout<<res<<endl;
	}
}