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>

using namespace std;

int nextInt() { int d; cin >> d; return d;}

bool fall(int field[][5], int H) {
  bool isFalledRet = false;
  
  for (int y = H - 1; y >= 0; y--) {
    for (int x = 0; x < 5; x++) {
      if (field[y][x] != 0) {
        int i;
        bool isFalled = false;
        for (i = y + 1; i < H; i++) {
          if (field[i][x] != 0) {
            break;
          }
          isFalled = true;
        }
        if (isFalled) {
          field[i - 1][x] = field[y][x];
          field[y][x] = 0;
          isFalledRet = true;
        }
      }
    }
  }
  
  return isFalledRet;
}

int dfs(int field[][5], int H) {
  // テヲツーツエテ・ツケツウテ」ツ?ォ3テ」ツ?、テゥツ?」テァツカツ堙」ツ?療」ツ?ヲテ」ツつ凝」ツつづ」ツ?ョテ」ツつ津ヲツ篠「テ」ツ??
  int score = 0;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < 3; x++) {
      if (x < 1) {
        if (field[y][0] != 0 && field[y][0] == field[y][1] && field[y][1] == field[y][2] &&
              field[y][2] == field[y][3] && field[y][3] == field[y][4]) {
          score += 5 * field[y][0];
          field[y][0] = 0;
          field[y][1] = 0;
          field[y][2] = 0;
          field[y][3] = 0;
          field[y][4] = 0;        
        }  
      }
      if (x < 2) {
        if (field[y][x] != 0 && field[y][x] == field[y][x + 1] && field[y][x + 1] == field[y][x + 2] && field[y][x + 2] == field[y][x + 3]) {
          score += 4 * field[y][x];
          field[y][x]     = 0;
          field[y][x + 1] = 0;
          field[y][x + 2] = 0;
          field[y][x + 3] = 0;
        }
      }     
      if (field[y][x] != 0 && field[y][x] == field[y][x + 1] && field[y][x + 1] == field[y][x + 2]) {
        score += 3 * field[y][x];
        field[y][x]     = 0;
        field[y][x + 1] = 0;
        field[y][x + 2] = 0;
      }
    }  
  }
  
  bool isFalled = fall(field, H);
  if (!isFalled) {
    return score;
  }
  
  int dfsScore = dfs(field, H);
  score += dfsScore;
  return score;
}

int main() {
  while (true) {
    int H = nextInt();
    if (H == 0) {
      break;
    }
    
    int field[H][5];
    memset(field, 0, sizeof(field));
    
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < 5; x++) {
        int d = nextInt();
        field[y][x] = d;
      }
    }
    int ans = dfs(field, H);
    cout << ans << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,a,n) for(int i=(a); i<(n); i++)
#define all(v) v.begin(), v.end()
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<int ,int>;
const int INF = 1e9;
const int MOD = 1e9+7;

int main() {
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());

    while (true) {
        int h; cin >> h;
        if(!h) break;

        vector<string> a(h);
        rep(i, h) {
            string tmp;
            rep(j, 5) {
                char x; cin >> x;
                tmp += x;
            }
            a[i] = tmp;
        }

        int ans = 0;
        bool fin = false;
        while (!fin) {
            fin = true;
            rep(i, h) {
                string row = a[h-i-1];
                FOR(num, 1, 10) {
                    if (count(all(row), num+'0')==5) {
                        ans += (num)*5;
                        fin = false;
                        rep(j, 5) a[h-i-1][j] = 'x';
                    } else if (count(all(row), num+'0')==4) {
                        bool found = false;
                        rep(j, 2) {
                            if (row[j+0]==row[j+1] && row[j+1]==row[j+2] && row[j+2]==row[j+3]) {
                                ans += num*4;
                                fin = false;
                                found = true;
                                rep(k, 4) a[h-i-1][j+k] = 'x';
                            }
                        }
                        if (!found) {
                            rep(j, 3) {
                                if (row[j+0]==row[j+1] && row[j+1]==row[j+2]) {
                                    ans += num*3;
                                    fin = false;
                                    rep(k, 3) a[h-i-1][j+k] = 'x';
                                }
                            }
                        }
                    } else if (count(all(row), num+'0')==3) {
                        rep(j, 3) {
                            if (row[j+0]==row[j+1] && row[j+1]==row[j+2]) {
                                ans += num*3;
                                fin = false;
                                rep(k, 3) a[h-i-1][j+k] = 'x';
                            }
                        }
                    }
                }
            }
            if (!fin) {
                rep(_, h) {
                    for(int i=h-1;i>0;i--) rep(j, 5) {
                        if (a[i][j]=='x' && a[i-1][j]!='x') swap(a[i][j], a[i-1][j]);
                    }
                }
            }
        }
        cout << ans << endl;
    }
}