Problem:


Property Distribution

 タナカ氏が HW アールの果樹園を残して亡くなりました。果樹園は東西南北方向に H &times; W の区画に分けられ、区画ごとにリンゴ、カキ、ミカンが植えられています。タナカ氏はこんな遺言を残していました。

      果樹園は区画単位でできるだけ多くの血縁者に分けること。ただし、ある区画の東西南北どれかの方向にとなりあう区画に同じ種類の果物が植えられていた場合は、区画の境界が分からないのでそれらは 1 つの大きな区画として扱うこと。

 例えば次のような 3 &times; 10 の区画であれば ('リ'はリンゴ、'カ'はカキ、'ミ'はミカンを表す)

同じ樹がある区画の間の境界を消すと次のようになり、

 結局 10 個の区画、つまり 10 人で分けられることになります。

雪が降って区画の境界が見えなくなる前に分配を終えなくてはなりません。あなたの仕事は果樹園の地図をもとに分配する区画の数を決めることです。

果樹園の地図を読み込み、分配を受けられる血縁者の人数を出力するプログラムを作成してください。

Input

複数のデータセットが与えられます。各データセットは空白で区切られた H, W (H, W ≤ 100) を含む行から始まり、続いて H &times; W の文字からなる H 行の文字列が与えられます。この文字列には、リンゴを表す '@'、カキを表す '#'、ミカンを表す '*'、の 3 文字しか現れません。

入力はゼロが２つの行で終わります。データセットの数は 20 を超えません。

Output

各データセットごとに、分配を受ける人数を１行に出力してください。

Sample Input

10 10
####*****@
@#@@@@#*#*
@##***@@@*
#****#*@**
##@*#@@*##
*@@@@*@@@#
***#@*@##*
*@@@*@@##@
*@*#*@##**
@****#@@#@
0 0

Output for the Sample Input

33



Sample input:
10 10
####*****@
@#@@@@#*#*
@##***@@@*
#****#*@**
##@*#@@*##
*@@@@*@@@#
***#@*@##*
*@@@*@@##@
*@*#*@##**
@****#@@#@
0 0


Sample output:
33

Submission status: Runtime Error

Solution code:
h = 0
w = 0
list1 = []

def dfs(x, y, f):
    list1[y] = list1[y][:x] + '1' + list1[y][x+1:]

    if y - 1 >= 0:
        if list1[y - 1][x] == f:
            dfs(x, y - 1, f)
    if y + 1 < h:
        if list1[y + 1][x] == f:
            dfs(x, y + 1, f)
    if x - 1 >= 0:
        if list1[y][x - 1] == f:
            dfs(x - 1, y, f)
    if x + 1 < w:
        if list1[y][x + 1] == f:
            dfs(x + 1, y, f)
    return 1

while True:
    h, w = list(map(lambda x: int(x),input().split(" ")))
    if 0 in (h, w):
        break
    n = 0
    for i in range(0, h):
        list1.append(input())
    for i in range(0, h):
        for j in range(0, w):
            if list1[i][j] != '1':
                n += dfs(j, i, list1[i][j])
    print(n)
    list1=[]

Submission status: Runtime Error

Solution code:
h = 0
w = 0
list1 = []

def dfs(x, y, f):
    list1[y] = list1[y][:x] + '1' + list1[y][x+1:]

    if y - 1 >= 0:
        if list1[y - 1][x] == f:
            dfs(x, y - 1, f)
    if y + 1 < h:
        if list1[y + 1][x] == f:
            dfs(x, y + 1, f)
    if x - 1 >= 0:
        if list1[y][x - 1] == f:
            dfs(x - 1, y, f)
    if x + 1 < w:
        if list1[y][x + 1] == f:
            dfs(x + 1, y, f)
    return

while True:
    h, w = list(map(lambda x: int(x),input().split(" ")))
    if 0 in (h, w):
        break
    n = 0
    for i in range(0, h):
        list1.append(input())
    for i in range(0, h):
        for j in range(0, w):
            if list1[i][j] != '1':
                dfs(j, i, list1[i][j])
                n+=1
    print(n)
    list1=[]

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python


matrix = []
vec = [[1,0],[-1,0],[0,1],[0,-1]]
visited =[]

def dfs(x,y,fruit):
  visited.append((x,y))
  for v in vec:
    nx = x + v[0]
    ny = y + v[1]
    if not (nx,ny) in visited and nx in xrange(w) and ny in xrange(h):
      if matrix[ny][nx] == fruit:
        dfs(nx,ny,fruit)



while True:
  h,w = map(int,raw_input().rstrip().split(" "))
  if h == 0 and w == 0:
    break
  matrix = []
  visited = []
  ans = 0
  for i in xrange(h):
    s = raw_input().rstrip()
    matrix.append(list(s))
  for i in xrange(h):
    for j in xrange(w):
      if not (j,i) in visited:
        dfs(j,i,matrix[i][j])
        ans+=1
  print ans

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
import sys

sys.setrecursionlimit(10000)


matrix = []
vec = [[1,0],[-1,0],[0,1],[0,-1]]
visited =[]

def dfs(x,y,fruit):
  visited.append((x,y))
  for v in vec:
    nx = x + v[0]
    ny = y + v[1]
    if not (nx,ny) in visited and nx in xrange(w) and ny in xrange(h):
      if matrix[ny][nx] == fruit:
        dfs(nx,ny,fruit)



while True:
  h,w = map(int,raw_input().rstrip().split(" "))
  if h == 0 and w == 0:
    break
  matrix = []
  visited = []
  ans = 0
  for i in xrange(h):
    s = raw_input().rstrip()
    matrix.append(list(s))
  for i in xrange(h):
    for j in xrange(w):
      if not (j,i) in visited:
        dfs(j,i,matrix[i][j])
        ans+=1
  print ans

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
import sys
sys.setrecursionlimit(10000)

matrix = []
vec = [[1,0],[-1,0],[0,1],[0,-1]]
visited =[]
h = 0
w = 0

def dfs(x,y,fruit):
  global visited
  visited.append((x,y))
  for v in vec:
    nx = x + v[0]
    ny = y + v[1]
    if (not (nx,ny) in visited) and nx in xrange(w) and ny in xrange(h):
      if matrix[ny][nx] == fruit:
        dfs(nx,ny,fruit)
  return 0


while True:
  h,w = map(int,raw_input().rstrip().split(" "))
  if h == 0 and w == 0:
    break
  matrix = []
  visited = []
  ans = 0
  for i in xrange(h):
    s = raw_input().rstrip()
    matrix.append(list(s))
  for i in xrange(h):
    for j in xrange(w):
      if not (j,i) in visited:
        dfs(j,i,matrix[i][j])
        ans+=1
  print ans

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python


matrix = []
vec = [[1,0],[-1,0],[0,1],[0,-1]]
visited =[]
h = 0
w = 0

def dfs(x,y,fruit):
  #visited.append((x,y))
  visited[y][x] = 1
  for v in vec:
    nx = x + v[0]
    ny = y + v[1]
    if nx in xrange(w) and ny in xrange(h):
      if visited[ny][nx]==0 and matrix[ny][nx] == fruit:
        dfs(nx,ny,fruit)
  return 0


while True:
  h,w = map(int,raw_input().split())
  if h == 0 and w == 0:
    break
#  print h,w
  matrix = []
  visited = []
  ans = 0
  for i in xrange(h):
    s = raw_input().rstrip()
    matrix.append(list(s))
    visited.append([0]*w)
  for i in xrange(h):
    for j in xrange(w):
      #if not (j,i) in visited:
      if visited[i][j] == 0:
        dfs(j,i,matrix[i][j])
        ans+=1
  print ans

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
import sys

sys.setrecursionlimit(10000)

matrix = []
vec = [[1,0],[-1,0],[0,1],[0,-1]]
visited =[]
h = 0
w = 0

def dfs(x,y,fruit):
  #visited.append((x,y))
  visited[y][x] = 1
  for v in vec:
    nx = x + v[0]
    ny = y + v[1]
    if nx in xrange(w) and ny in xrange(h):
      if visited[ny][nx]==0 and matrix[ny][nx] == fruit:
        dfs(nx,ny,fruit)
  return 0


while True:
  h,w = map(int,raw_input().split())
  if h == 0 and w == 0:
    break
#  print h,w
  matrix = []
  visited = []
  ans = 0
  for i in xrange(h):
    s = raw_input().rstrip()
    matrix.append(list(s))
    visited.append([0]*w)
  for i in xrange(h):
    for j in xrange(w):
      #if not (j,i) in visited:
      if visited[i][j] == 0:
        dfs(j,i,matrix[i][j])
        ans+=1
  print ans

Submission status: Runtime Error

Solution code:
dxy = [[1, 0], [0, 1], [-1, 0], [0, -1]]
field = []
H, W = 0, 0
def solver(x, y, symbol):
  global field, H, W
  if x < 0 or y < 0 or x >= H or y >= W:
    return
  for dx, dy in dxy:
    if x+dx < 0 or y+dy < 0 or x+dx >= H or y+dy >= W:
      continue
    if field[x+dx][y+dy] == symbol:
      field[x+dx][y+dy] = '.'
      solver(x+dx, y+dy, symbol)

while True:
  H, W = map(int, raw_input().split())
  if H == 0 and W == 0:
    break
  field = [list(raw_input()) for i in range(H)]

  ans = 0
  for x in xrange(H):
    for y in xrange(W):
      if field[x][y] != '.':
        solver(x, y, field[x][y])
        ans += 1
  print ans

Submission status: Runtime Error

Solution code:
dxy = [[1, 0], [0, 1], [-1, 0], [0, -1]]
field = []
H, W = 0, 0

def solver(x, y, symbol):
  global field, H, W
  if x < 0 or y < 0 or x >= H or y >= W:
    return
  for dx, dy in dxy:
    if x+dx < 0 or y+dy < 0 or x+dx >= H or y+dy >= W:
      continue
    if field[x+dx][y+dy] == symbol:
      field[x+dx][y+dy] = '.'
      solver(x+dx, y+dy, symbol)

while True:
  H, W = map(int, raw_input().split())
  if H == 0 and W == 0:
    break
  field = [list(raw_input()) for i in range(H)]

  ans = 0
  for x in xrange(H):
    for y in xrange(W):
      if field[x][y] != '.':
        solver(x, y, field[x][y])
        ans += 1
  print ans

Submission status: Runtime Error

Solution code:
dxy = [[1, 0], [0, 1], [-1, 0], [0, -1]]
field = []
H, W = 0, 0
q = queue.Queue()

def solver(x, y, symbol):
  global q, field, H, W
  while True:
    if x < 0 or y < 0 or x >= H or y >= W:
      continue
    for dx, dy in dxy:
      if x+dx < 0 or y+dy < 0 or x+dx >= H or y+dy >= W:
        continue
      if field[x+dx][y+dy] == symbol:
        field[x+dx][y+dy] = '.'
        q.put([x+dx, y+dy])
    data = q.get()
    if data is None:
      return
    x, y = data)

while True:
  H, W = map(int, raw_input().split())
  if H == 0 and W == 0:
    break
  field = [list(raw_input()) for i in range(H)]

  ans = 0
  for x in xrange(H):
    for y in xrange(W):
      if field[x][y] != '.':
        solver(x, y, field[x][y])
        ans += 1
  print ans

Submission status: Runtime Error

Solution code:
count=0
a_0=[]
a_1=[]
mode=0

init_input=raw_input().split()
n=init_input[0]
m=init_input[1]

a_0=raw_input().split()
for i in range(m):
 if i==0:
  count+=1
 else:
  if a_0[i]!=a_0[i-1]:
   count+=1
mode=1
n-=1

while n>0:
 if mode==0:
  a_0=raw_input().split()
  for i in range(m):
   if a_0[i]!=a_1[i]:
    if i==0:
     count+=1
    else:
     if a_0[i]!=a_0[i-1]:
      count+=1
  mode=1
  n-=1
 else:
  a_1=raw_input().split()
  for i in range(m):
   if a_1[i]!=a_0[i]:
    if i==0:
     count+=1
    else:
     if a_1[i]!=a_1[i-1]:
      count+=1
  mode=0
  n-=1

print count

Submission status: Runtime Error

Solution code:
count=0
a_0=[]
a_1=[]
mode=0

init_input=raw_input().split()
n=init_input[0]
m=init_input[1]

a_0=raw_input().split()
for i in range(m):
	if i==0:
		count+=1
	else:
		if a_0[i]!=a_0[i-1]:
			count+=1
mode=1
n-=1

while n>0:
	if mode==0:
		a_0=raw_input().split()
		for i in range(m):
			if a_0[i]!=a_1[i]:
				if i==0:
					count+=1
				else:
					if a_0[i]!=a_0[i-1]:
						count+=1
		mode=1
		n-=1
	else:
		a_1=raw_input().split()
		for i in range(m):
			if a_1[i]!=a_0[i]:
				if i==0:
					count+=1
				else:
					if a_1[i]!=a_1[i-1]:
						count+=1
		mode=0
		n-=1

print count

Submission status: Runtime Error

Solution code:
count=0
a_0=[]
a_1=[]
mode=0

init_input=map(int, raw_input().split())
n=init_input[0]
m=init_input[1]

a_0=raw_input().split()
for i in range(m):
	if i==0:
		count+=1
	else:
		if a_0[i]!=a_0[i-1]:
			count+=1
mode=1
n-=1

while n>0:
	if mode==0:
		a_0=raw_input().split()
		for i in range(m):
			if a_0[i]!=a_1[i]:
				if i==0:
					count+=1
				else:
					if a_0[i]!=a_0[i-1]:
						count+=1
		mode=1
		n-=1
	else:
		a_1=raw_input().split()
		for i in range(m):
			if a_1[i]!=a_0[i]:
				if i==0:
					count+=1
				else:
					if a_1[i]!=a_1[i-1]:
						count+=1
		mode=0
		n-=1

print count

Submission status: Runtime Error

Solution code:
count=0
a_0=[]
a_1=[]
mode=0

init_input=map(int, raw_input().split())
n=init_input[0]
m=init_input[1]

line=input()
a_0=list(line)
for i in range(m):
	if i==0:
		count+=1
	else:
		if a_0[i]!=a_0[i-1]:
			count+=1
mode=1
n-=1

while n>0:
	line=input()
	if mode==0:
		a_0=list(line)
		for i in range(m):
			if a_0[i]!=a_1[i]:
				if i==0:
					count+=1
				else:
					if a_0[i]!=a_0[i-1]:
						count+=1
		mode=1
		n-=1
	else:
		a_1=list(line)
		for i in range(m):
			if a_1[i]!=a_0[i]:
				if i==0:
					count+=1
				else:
					if a_1[i]!=a_1[i-1]:
						count+=1
		mode=0
		n-=1

print count

Submission status: Runtime Error

Solution code:
while True:
    H, W = list(map(int, input().split()))
    if H == 0 and W == 0:
        break
    field = []
    for i in range(H):
        field.append(list(input()))
    ans = 0

    def field_count(x, y):
        fruit = field[x][y]
        field[x][y] = "."
        for dx in [-1, 0, 1]:
            nx = x + dx
            if 0 <= nx < H and field[nx][y] == fruit:
                field_count(nx, y)
        for dy in [-1, 0, 1]:
            ny = y + dy
            if 0 <= ny < W and field[x][ny] == fruit:
                field_count(x, ny)

    for i in range(H):
        for j in range(W):
            if field[i][j] != ".":
                field_count(i, j)
                ans += 1
    print(str(ans))

Submission status: Runtime Error

Solution code:
def dfs(h, w, m, v, H, W):
    '''
    m[h][w]?????????????????????????¨???????(v???True?????????)
    '''
    v[h][w] = True
    for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        nh = h + dh
        nw = w + dw
        if nh in range(H) and nw in range(W) and m[nh][nw] == m[h][w] and v[nh][nw] == False:
            dfs(nh, nw, m, v, H, W)


while True:
    H, W = map(int, input().split())
    if H == 0 and W == 0:
        break
    else:
        m = [list(input()) for _ in range(H)]
        v = [[False for _ in range(W)] for __ in range(H)]
        count = 0

        for h in range(H):
            for w in range(W):
                if v[h][w] == False:
                    count += 1
                    dfs(h, w, m, v, H, W)

        print(count)

Submission status: Runtime Error

Solution code:
def dfs(h, w, m, v, H, W):
    '''
    m[h][w]?????????????????????????¨???????(v???True?????????)
    '''
    v[h][w] = True
    for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        nh = h + dh
        nw = w + dw
        if nh in range(H) and nw in range(W) and m[nh][nw] == m[h][w] and v[nh][nw] == False:
            dfs(nh, nw, m, v, H, W)

while True:
    H, W = list(map(int, input().split()))
    if H == 0 and W == 0:
        break

    m = [input() for _ in range(H)]
    v = [[False for _ in range(W)] for __ in range(H)]
    count = 0

    for h in range(H):
        for w in range(W):
            if v[h][w] == False:
                count += 1
                dfs(h, w, m, v, H, W)

    print(count)

Submission status: Runtime Error

Solution code:
def dfs(h, w, m, v, H, W):
    '''
    m[h][w]?????????????????????????¨???????(v???True?????????)
    '''
    v[h][w] = True
    for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        nh = h + dh
        nw = w + dw
        if nh in range(H) and nw in range(W) and m[nh][nw] == m[h][w] and v[nh][nw] == False:
            dfs(nh, nw, m, v, H, W)

ms, Hs, Ws = [], [], []
while True:
    H, W = list(map(int, input().split()))
    if H == 0 and W == 0:
        break
    Hs.append(H)
    Ws.append(W)

    m = [input() for _ in range(H)]
    ms.append(m)

for m, H, W in zip(ms, Hs, Ws):
    v = [[False for _ in range(W)] for __ in range(H)]
    count = 0

    for h in range(H):
        for w in range(W):
            if v[h][w] == False:
                count += 1
                dfs(h, w, m, v, H, W)

    print(count)

Submission status: Runtime Error

Solution code:
def solve(values,hp=0,vp=0,stamp=0):
	if hp==0:
	if hp!=0 and values[hp-1][vp]==values[hp][vp]:
		values[hp-1][vp]=stamp
		solve(values,hp-1,vp,stamp)
	if hp!=len(values) and values[hp+1][vp]==values[hp][vp]:
		values[hp+1][vp]=stamp
		solve(values,hp-1,vp,stamp)
	if vp!=0 and values[hp][vp-1]==values[hp][vp]:
		values[hp][vp]=stamp
		solve(values,hp-1,vp,stamp)
	if vp!=len(values[hp] and) values[hp][vp+1]==values[hp][vp]:
		values[hp][vp+1]=stamp
		solve(values,hp-1,vp,stamp)
	values[hp][vp]=stamp

	for i in range(values):
		for j in range(values[i]):
			if values[i][j] in ['@','#','*']:
				stamp+=1
				solve(values,i,j,stamp)
	return stamp


H,W = raw_input().split(' ')
values = list()
for _ in range(H):
	values.append(raw_input().split())
print(solve(values))

Submission status: Runtime Error

Solution code:
#! -*- coding:utf-8 -*-

def search(values,hp=0,vp=0,stamp=0,fruit):
	if not (0<=hp<len(values)): return
	if not (0<=vp<len(values[0])): return
	if fruit!=values[hp][vp]: return
	fruit,values[hp][vp]=values[hp][vp],stamp
	search(values,hp-1,vp,stamp,fruit)
	search(values,hp+1,vp,stamp,fruit)
	search(values,hp,vp-1,stamp,fruit)
	search(values,hp,vp+1,stamp,fruit)

def solve(values):
	stamp=0
	for i in range(len(values)):
		for j in range(len(values[i])):
			if values[i][j] in ['@','#','*']:
				search(values,i,j,stamp+1,values[i][j])
				stamp+=1
	return stamp


H,W = list(map(int,input().split(' ')))
values = list()
for _ in range(H):
	values.append([x for x in input().strip()])
print(solve(values))

Submission status: Runtime Error

Solution code:
import sys


process()


def process():
    data = get_data()
    integer = data[0].split()
    H = int(integer[0])
    W = int(integer[1])
    data = data[1: -1]

    field = create_field(H, W)
    print search(H, W, data, field)


def search(H, W, data, field):
    count = 0
    for i in xrange(H):
        for j in xrange(W):
            if field[i][j] == 0:
                continue
            _search(i, j, H, W, data[i][j], data, field)
            count += 1
    return count


def _search(i, j, H, W, cell, data, field):
    field[i][j] = 0
    for a in xrange(i-1, i+2):
        if -1 < a < H:
            pass
        else:
            continue
        for b in xrange(j-1, j+2):
            if -1 < b < W:
                pass
            else:
                continue

            if a != i and b != j:
                continue

            if field[a][b] == 0:
                continue
            if cell == data[a][b]:
                _search(a, b, H, W, cell, data, field)
    return


def create_field(H, W):
    field = []
    for i in xrange(H):
        raw = []
        for j in xrange(W):
            raw.append(1)
        field.append(raw)
    return field


def get_data():
    return [line.strip() for line in sys.stdin.readlines()]

Submission status: Runtime Error

Solution code:
#! -*- coding:utf-8 -*-

def search(values,hp,vp,fruit):
    if not (0<=hp<len(values)): return
    if not (0<=vp<len(values[hp])): return
    if fruit!=values[hp][vp]: return
    values[hp][vp]=0
    search(values,hp-1,vp,fruit)
    search(values,hp+1,vp,fruit)
    search(values,hp,vp-1,fruit)
    search(values,hp,vp+1,fruit)

def solve(values):
    count=0
    for i in range(len(values)):
        for j in range(len(values[i])):
            if values[i][j] in ['@','#','*']:
                search(values,i,j,values[i][j])
                count+=1
    return count

if __name__=='__main__':
    line=input()
    while line!='0 0':
        H,W = list(map(int,line.split(' ')))
        values = list()
        for _ in range(H):
            values.append([x for x in input().strip()])
        print(solve(values))

Submission status: Runtime Error

Solution code:
#! -*- coding:utf-8 -*-

def search(values,hp,vp,fruit):
    if not (0<=hp<len(values)): return
    if not (0<=vp<len(values[hp])): return
    if fruit!=values[hp][vp]: return
    values[hp][vp]=0
    search(values,hp-1,vp,fruit)
    search(values,hp+1,vp,fruit)
    search(values,hp,vp-1,fruit)
    search(values,hp,vp+1,fruit)

def solve(values):
    count=0
    for i in range(len(values)):
        for j in range(len(values[i])):
            if values[i][j] in ['@','#','*']:
                search(values,i,j,values[i][j])
                count+=1
    return count

def main():
    line=input()
    values=list()
    while line!='0 0':
        H,W = list(map(int,line.split(' ')))
        value = list()
        for _ in range(H):
            value.append([x for x in input().strip()])
    for value in values:
        print(solve(value))

if __name__=='__main__':
    main():

Submission status: Runtime Error

Solution code:
#! -*- coding:utf-8 -*-

def search(values,hp,vp,fruit):
	if not (0<=hp<len(values)): return
	if not (0<=vp<len(values[hp])): return
	if fruit!=values[hp][vp]: return
	values[hp][vp]=0
	search(values,hp-1,vp,fruit)
	search(values,hp+1,vp,fruit)
	search(values,hp,vp-1,fruit)
	search(values,hp,vp+1,fruit)

def solve(values):
	count=0
	for i in range(len(values)):
		for j in range(len(values[i])):
			if values[i][j] in ['@','#','*']:
				search(values,i,j,values[i][j])
				count+=1
	return count

def main():
	line,values=input(),list()
	while line!='0 0':
		H,W = list(map(int,line.split(' ')))
		value = list()
		for _ in range(H):
			value.append([x for x in input().strip()])
		values.append(value)
	for value in values:
		print(solve(value))

if __name__=='__main__':
	main():

Submission status: Runtime Error

Solution code:
#! -*- coding:utf-8 -*-

def search(values,hp,vp,fruit):
    if not (0<=hp<len(values)): return
    if not (0<=vp<len(values[hp])): return
    if fruit!=values[hp][vp]: return
    values[hp][vp]=0
    search(values,hp-1,vp,fruit)
    search(values,hp+1,vp,fruit)
    search(values,hp,vp-1,fruit)
    search(values,hp,vp+1,fruit)

def solve(values):
    count=0
    for i in range(len(values)):
        for j in range(len(values[i])):
            if values[i][j] in ['@','#','*']:
                search(values,i,j,values[i][j])
                count+=1
    return count

def main():
    line,values=input(),list()
    while line!='0 0':
        H,W = list(map(int,line.split(' ')))
        value = list()
        for _ in range(H):
            value.append([x for x in input().strip()])
            values.append(value)
    for value in values:
        print(solve(value))

if __name__=='__main__':
    main():

Submission status: Runtime Error

Solution code:
#! -*- coding:utf-8 -*-

def search(values,hp,vp,fruit):
    if not (0<=hp<len(values)): return
    if not (0<=vp<len(values[hp])): return
    if fruit!=values[hp][vp]: return
    values[hp][vp]=0
    search(values,hp-1,vp,fruit)
    search(values,hp+1,vp,fruit)
    search(values,hp,vp-1,fruit)
    search(values,hp,vp+1,fruit)

def solve(values):
    count=0
    for i in range(len(values)):
        for j in range(len(values[i])):
            if values[i][j] in ['@','#','*']:
                search(values,i,j,values[i][j])
                count+=1
    return count

def main():
    line,values=input(),list()
    while line!='0 0':
        H,W = list(map(int,line.split(' ')))
        value = list()
        for _ in range(H):
            value.append([x for x in input().strip()])
        values.append(value)
    for value in values:
        print(solve(value))

if __name__=='__main__':
    main():

Submission status: Runtime Error

Solution code:
#! -*- coding:utf-8 -*-

def search(values,hp,vp,fruit):
    if not (0<=hp<len(values)): return
    if not (0<=vp<len(values[hp])): return
    if fruit!=values[hp][vp]: return
    values[hp][vp]=0
    search(values,hp-1,vp,fruit)
    search(values,hp+1,vp,fruit)
    search(values,hp,vp-1,fruit)
    search(values,hp,vp+1,fruit)

def solve(values):
    count=0
    for i in range(len(values)):
        for j in range(len(values[i])):
            if values[i][j] in ['@','#','*']:
                search(values,i,j,values[i][j])
                count+=1
    return count

def main():
    line,values=input(),list()
    while line!='0 0':
        H,W = list(map(int,line.split(' ')))
        value = list()
        for _ in range(H):
            value.append([x for x in input().strip()])
        values.append(value)
        line = input()
    for value in values:
        print(solve(value))

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
def process():
    data = []
    for line in get_data():
        if ' ' in line:
            integer = line.split()
            if integer[0] == integer[1] == 0:
                data = []
                continue
            H = int(integer[0])
            W = int(integer[1])
        else:
            data.append(line)

        if len(data) == H:
            field = create_field(H, W)
            print search(H, W, data, field)


def search(H, W, data, field):
    count = 0
    for i in xrange(H):
        for j in xrange(W):
            if field[i][j] == 0:
                continue
            _search(i, j, H, W, data[i][j], data, field)
            count += 1
    return count


def _search(i, j, H, W, cell, data, field):
    field[i][j] = 0
    for a in xrange(i-1, i+2):
        if -1 < a < H:
            pass
        else:
            continue
        for b in xrange(j-1, j+2):
            if -1 < b < W:
                pass
            else:
                continue

            if a != i and b != j:
                continue

            if field[a][b] == 0:
                continue
            if cell == data[a][b]:
                _search(a, b, H, W, cell, data, field)
    return


def create_field(H, W):
    field = []
    for i in xrange(H):
        raw = []
        for j in xrange(W):
            raw.append(1)
        field.append(raw)
    return field


def get_data():
    return [line.strip() for line in sys.stdin.readlines()]


if __name__ == '__main__':
    process()

Submission status: Runtime Error

Solution code:
#! -*- coding:utf-8 -*-

def search(values,hp,vp,fruit):
    if not (0<=hp<len(values)): return
    if not (0<=vp<len(values[hp])): return
    if fruit!=values[hp][vp]: return
    values[hp][vp]=0
    search(values,hp-1,vp,fruit)
    search(values,hp+1,vp,fruit)
    search(values,hp,vp-1,fruit)
    search(values,hp,vp+1,fruit)

def solve(values):
    count=0
    for i in range(len(values)):
        for j in range(len(values[i])):
            if values[i][j] in ['@','#','*']:
                search(values,i,j,values[i][j])
                count+=1
    return count

def main():
    line,values=input(),list()
    while line!='0 0':
        print(line)
        H,W = list(map(int,line.strip().split(' ')))
        value = list()
        for _ in range(H):
            value.append([x for x in input().strip()])
        values.append(value)
        line = input()
    for value in values:
        print(value)
        print(solve(value))

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
#! -*- coding:utf-8 -*-

def search(values,hp,vp,fruit):
    if not (0<=hp<len(values)): return
    if not (0<=vp<len(values[hp])): return
    if fruit!=values[hp][vp]: return
    values[hp][vp]=0
    search(values,hp-1,vp,fruit)
    search(values,hp+1,vp,fruit)
    search(values,hp,vp-1,fruit)
    search(values,hp,vp+1,fruit)

def solve(values):
    count=0
    for i in range(len(values)):
        for j in range(len(values[i])):
            if values[i][j] in ['@','#','*']:
                search(values,i,j,values[i][j])
                count+=1
    return count

def main():
    line,values=input(),list()
    while line!='0 0':
        H,W = list(map(int,line.strip().split(' ')))
        value = list()
        for _ in range(H):
            value.append([x for x in input().strip()])
        values.append(value)
        line = input()
    for value in values:
        print(solve(value))

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
#! -*- coding:utf-8 -*-

def search(values,hp,vp,fruit):
	if not (0<=hp<len(values)): return
	if not (0<=vp<len(values[hp])): return
	if fruit!=values[hp][vp]: return
	values[hp][vp]=0
	search(values,hp-1,vp,fruit)
	search(values,hp+1,vp,fruit)
	search(values,hp,vp-1,fruit)
	search(values,hp,vp+1,fruit)

def solve(values):
	count=0
	for i in range(len(values)):
		for j in range(len(values[i])):
			if values[i][j] in ['@','#','*']:
				search(values,i,j,values[i][j])
				count+=1
	return count

def main():
	line,values=input(),list()
	while line!='0 0':
		H,W = list(map(int,line.strip().split(' ')))
		value = list()
		for _ in range(H):
			value.append([x for x in input().strip()])
		values.append(value)
		line = input()
	for value in values:
		print(solve(value))

if __name__=='__main__':
	main()

Submission status: Runtime Error

Solution code:
def process():
    data = []
    for line in get_data():
        line = line.split()
        if len(line) == 2:
            if line[0] == line[1] == '0':
                data = []
                continue
            H = int(line[0])
            W = int(line[1])
        elif len(line) == 0:
            pass
        else:
            data.append(line[0])
            if len(data) == H:
                field = create_field(H, W)
                print search(H, W, data, field)


def search(H, W, data, field):
    count = 0
    for i in xrange(H):
        for j in xrange(W):
            if field[i][j] == 0:
                continue
            _search(i, j, H, W, data[i][j], data, field)
            count += 1
    return count


def _search(i, j, H, W, cell, data, field):
    field[i][j] = 0
    for a in xrange(i-1, i+2):
        if -1 < a < H:
            pass
        else:
            continue
        for b in xrange(j-1, j+2):
            if -1 < b < W:
                pass
            else:
                continue

            if a != i and b != j:
                continue

            if field[a][b] == 0:
                continue
            if cell == data[a][b]:
                _search(a, b, H, W, cell, data, field)
    return


def create_field(H, W):
    field = []
    for i in xrange(H):
        raw = []
        for j in xrange(W):
            raw.append(1)
        field.append(raw)
    return field


def get_data():
    return [line.strip() for line in sys.stdin.readlines()]


if __name__ == '__main__':
    process()

Submission status: Runtime Error

Solution code:
import sys


def process():
    data = []
    for line in get_data():
        line = line.split()
        if len(line) == 2:
            if line[0] == line[1] == '0':
                data = []
                continue
            H = int(line[0])
            W = int(line[1])
        elif len(line) == 0:
            pass
        else:
            data.append(line[0])
            if len(data) == H:
                field = create_field(H, W)
                print search(H, W, data, field)


def search(H, W, data, field):
    count = 0
    for i in xrange(H):
        for j in xrange(W):
            if field[i][j] == 0:
                continue
            _search(i, j, H, W, data[i][j], data, field)
            count += 1
    return count


def _search(i, j, H, W, cell, data, field):
    field[i][j] = 0
    for a in xrange(i-1, i+2):
        if -1 < a < H:
            pass
        else:
            continue
        for b in xrange(j-1, j+2):
            if -1 < b < W:
                pass
            else:
                continue

            if a != i and b != j:
                continue

            if field[a][b] == 0:
                continue
            if cell == data[a][b]:
                _search(a, b, H, W, cell, data, field)
    return


def create_field(H, W):
    field = []
    for i in xrange(H):
        raw = []
        for j in xrange(W):
            raw.append(1)
        field.append(raw)
    return field


def get_data():
    return [line.strip() for line in sys.stdin.readlines()]


if __name__ == '__main__':
    process()

Submission status: Runtime Error

Solution code:
#! -*- coding:utf-8 -*-

def search(values,hp,vp,fruit):
	if not (0<=hp<len(values)): return
	if not (0<=vp<len(values[0])): return
	if fruit!=values[hp][vp]: return
	values[hp][vp]=0
	search(values,hp-1,vp,fruit)
	search(values,hp+1,vp,fruit)
	search(values,hp,vp-1,fruit)
	search(values,hp,vp+1,fruit)

def solve(values):
	count=0
	for i in range(len(values)):
		for j in range(len(values[i])):
			if values[i][j] in ['@','#','*']:
				search(values,i,j,values[i][j])
				count+=1
	return count

def main():
	line,values=input(),list()
	while line!='0 0':
		H,W = list(map(int,line.strip().split(' ')))
		value = list()
		for _ in range(H):
			value.append([x for x in input().strip()])
		values.append(value)
		line = input()
	for value in values:
		print(solve(value))

if __name__=='__main__':
	main()