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)
}

Python: from collections import deque    

def cnt(aa):
    for i in range(3):
        if aa[i]!=0 and aa[i]==aa[i+1] and aa[i+1]==aa[i+2]:
            n=aa[i]
            j=i
            k=3
            if i+3<5 and aa[i+2]==aa[i+3]:
                k=4
                if i+4<5 and aa[i+3]==aa[i+4]:
                    k=5
            return n,j,k                             
    return 0,0,0                        
                             
while True:
    h=int(input())
    if h==0:
        break
    a=[[0]*5 for i in range(h)]
    for i in range(h):
        a[i]=list(map(int,input().split()))
#    a=[[5, 9, 5, 5, 9], [5, 5, 6, 9, 9], [4, 6, 3, 6, 9], [3, 3, 2, 9, 9], [2, 2, 1, 1, 1]]
    a.reverse()
#    print(a)
    p=0
    while True:
        dq = deque()
        for i in range(h):
            n,j,k=cnt(a[i])
            p+=n*k
            if n!=0:
                dq.append((i,j,k))
#        print(dq)
        if len(dq)==0 :
            break
        while dq:
            hi,j,k=dq.popleft()
#            print("1:",a)
            for jj in range(j,j+k):
                a[hi][jj]=0
#        print("2:",a)
        for jj in range(5):
            for ii in range(h):
                if a[ii][jj]==0:
                    for iii in range(ii+1,h):
                        if a[iii][jj]!=0:
                            a[ii][jj]=a[iii][jj]
                            a[iii][jj]=0
                            break
#        print(a)
    print(p)

Python: class Puzzle():
  def __init__(self, mat, N):
    self.mat = mat
    self.h = N
    self.score = 0

  def calc(self):
    tmp_score = self.score

    for i in range(self.h):
      for j in range(3):
        if self.mat[i][j] > 0:
          # 5,2
          for k in range(5, 2, -1):
            if j+k <= 7 and k-j >= 3:
              s = set(self.mat[i][j:k])
              if len(s) == 1 and -1 not in s:
                tmp_score += list(s)[0] * (k-j)
                for l in range(j, k):
                  self.mat[i][l] = -1
                break

    if self.score >= tmp_score:
      return False
    else:
      self.score = tmp_score
      return True

  def flatten(self):
    pivot_mat = []
    for i in range(5):
      row = []
      for j in range(self.h):
        if self.mat[j][i] != -1:
          row.append(self.mat[j][i])

      short = self.h - len(row)
      if short > 0:
        row.reverse()
        for k in range(short):
          row.append(-1)
        row.reverse()
      
      pivot_mat.append(row)

    mat = [ [-1]*5 for i in range(self.h) ]
    for i in range(self.h):
      for j in range(5):
        mat[i][j] = pivot_mat[j][i]

    self.mat = mat

while True:
  h = int(input())
  if h==0: exit()
  mat = []
  for _ in range(h):
    mat.append(list(map(int,input().split())))
  puzzle = Puzzle(mat, h)

  while True:
    if puzzle.calc():
      puzzle.flatten()
    else:
      break

  print(puzzle.score)

Python: from itertools import zip_longest

while True:
    n = int(input())
    if not n:
        break
    field = [list(map(int, input().split())) for _ in range(n)]
    field.reverse()
    score = 0
    while True:
        no_more_disappear = True

        for y, row in enumerate(field):
            for i, stone in enumerate(row):
                if stone is None:
                    continue
                cnt = 1
                for stone2 in row[i + 1:]:
                    if stone != stone2:
                        break
                    cnt += 1
                if cnt >= 3:
                    row[i:i + cnt] = [None] * cnt
                    score += stone * cnt
                    no_more_disappear = False

        if no_more_disappear:
            break

        fieldT = map(lambda row: filter(None, row), zip(*field))
        field = map(list, zip_longest(*fieldT))

    print(score)

Python: a = int(input())
global s
s = 0

def de(ppp,h):
	re = False
	co  = 0
	global s
	for lo in ppp:
		for j ,cell in enumerate(lo):
			k = j
			p = j
			if k <= 2:
				while cell == lo[k+1] and cell != None:
					co += 1
					if k == 3:
						j = k+1
						break
					k += 1
					j = k
				if co >= 2:
					re = True
					s += (co+1)*cell
					for l in range(5):
						if l >= p and l <= j:
							lo[l] = None
				co = 0

	return re



def mo(ppp,h):
	for i ,roe in enumerate(ppp):
		for j , cell in enumerate(roe):
			kkk = i
			while i != 0 and ppp[kkk-1][j] == None:
				ppp[kkk-1][j] = ppp[kkk][j]
				ppp[kkk][j] = None
				if kkk == 1:
					break
				kkk -= 1


while a != 0:
	ppp = [list(map(int,input().split())) for i in range(a)]
	ppp.reverse()
	while(de(ppp,a) == True):
		mo(ppp,a)
	print(ppp)
	print(s)

	s = 0

	a = int(input())