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: Wrong Answer

Solution code:
def solve(c,h,w,count):
	if 0 <= h < H and 0 <= w < W:
		if c in "#@*" and c == A[h][w]:
			A[h][w] = count
			solve(c,h+1,w,count)
			solve(c,h-1,w,count)
			solve(c,h,w+1,count)
			solve(c,h,w-1,count)

while 1:
	H,W = map(int,raw_input().split())
	if H == W == 0:
		break
	A = [list(raw_input()) for i in range(H)]
	count = 0
	for h in range(H):
		for w in range(W):
			if str(A[h][w]) in "#@*":
				try:
					solve(A[h][w],h ,w,count)
					count += 1
				except:
					pass
	print count

Submission status: Wrong Answer

Solution code:
def solve(c,h,w,count):
	if 0 <= h < H and 0 <= w < W:
		if type(c) is not int and c == A[h][w]:
			A[h][w] = count
			solve(c,h+1,w,count)
			solve(c,h-1,w,count)
			solve(c,h,w+1,count)
			solve(c,h,w-1,count)

while 1:
	H,W = map(int,raw_input().split())
	if H == W == 0:
		break
	A = [list(raw_input()) for i in range(H)]
	count = 0
	for h in range(H):
		for w in range(W):
			if type(A[h][w]) is not int:
				#solve(A[h][w],h ,w,count)
				count += 1
	print count

Submission status: Wrong Answer

Solution code:
def solve(c,h,w,count):
	if 0 <= h < H and 0 <= w < W:
		if type(c) is not int and c == A[h][w]:
			A[h][w] = count
			for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]:
				try:solve(c,h+dh,w+dw,count)
				except:pass

while 1:
	H,W = map(int,raw_input().split())
	if H == W == 0:
		break
	A = [list(raw_input()) for i in range(H)]
	count = 0
	for h in range(H):
		for w in range(W):
			if type(A[h][w]) is not int:
				solve(A[h][w],h ,w,count)
				count += 1
	print count

Submission status: Wrong Answer

Solution code:
def solve(c,h,w,count):
	if not (0 <= h < H and 0 <= w < W):
		return
	if c == A[h][w]:
		A[h][w] = count
		for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]:
			try:solve(c,h+dh,w+dw,count)
			except:continue
while 1:
	H,W = map(int,raw_input().split())
	if H == W == 0:break
	A = [list(raw_input()) for i in range(H)]
	count = 0
	for h in range(H):
		for w in range(W):
			if type(A[h][w]) is not int:
				solve(A[h][w],h ,w,count)
				count += 1
	print count

Submission status: Wrong Answer

Solution code:
def solve(c,h,w):
	if not (0 <= h < H and 0 <= w < W):
		return
	if c == A[h][w]:
		A[h][w] = "X"
		for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]:
			try:solve(c,h+dh,w+dw)
			except:continue
while 1:
	H,W = map(int,raw_input().split())
	if H == W == 0:break
	A = [list(raw_input()) for i in range(H)]
	count = 0
	for h in range(H):
		for w in range(W):
			if A[h][w] != "X":
				solve(A[h][w],h,w)
				count += 1
	print count

Submission status: Wrong Answer

Solution code:
def f(y,x,c0):
  global A
  c=A[y][x]
  if c==" " or c!=c0:
    return 0
  else:
    A[y][x]=" "
    for i,j in [[0,-1],[0,1],[-1,0],[1,0]]:
      f(y+i,x+j,c0)
    return 1

while 1:
  H,W=map(int,raw_input().split())
  if H==W==0: break
  A=[[[] for _ in range(W+2)] for _ in range(H+2)]
  for i in range(H+2):
    if i==0 or i==H+1:
      for j in range(W+2): A[i][j]=" "
    else:
      s=" "+raw_input()+" "
      for j in range(W+2): A[i][j]=" "
  for e in A: print e
  c=0
  for i in range(1,H+1):
    for j in range(1,W+1):
      c+=f(i,j,A[i][j])
  print c

Submission status: Wrong Answer

Solution code:
def f(y,x,c0):
  global A
  c=A[y][x]
  if c=="X" or c!=c0:
    return 0
  else:
    A[y][x]="X"
    for i,j in [[0,-1],[0,1],[-1,0],[1,0]]:
      f(y+i,x+j,c0)
    return 1

while 1:
  H,W=map(int,raw_input().split())
  if H==W==0: break
  A=[["X" for _ in range(W+2)] for _ in range(H+2)]
  for i in range(H):
    s=raw_input()
    for j in range(W): A[i+1][j+1]=s[j]
  c=0
#  for i in range(1,H+1):
#    for j in range(1,W+1):
#      c+=f(i,j,A[i][j])
  print c

Submission status: Wrong Answer

Solution code:
def f(y,x,c0):
  global A
  c=A[y][x]
  if c=="X" or c!=c0:
    return 0
  else:
    A[y][x]="X"
    for i,j in [[0,-1],[0,1],[-1,0],[1,0]]:
      f(y+i,x+j,c0)
    return 1

while 1:
  H,W=map(int,raw_input().split())
  if H==W==0: break
  A=[["X" for _ in range(W+2)] for _ in range(H+2)]
  for i in range(H):
    s=raw_input()
    for j in range(W): A[i+1][j+1]=s[j]
  c=0
  for i in range(1,H+1):
    for j in range(1,W+1):
      c+=1#A[i][j]
#      c+=f(i,j,A[i][j])
  print c

Submission status: Wrong Answer

Solution code:
def f(y,x,c0):
  global A
  c=A[y][x]
  return 0
  if c=="X" or c!=c0:
    return 0
  else:
    A[y][x]="X"
    for i,j in [[0,-1],[0,1],[-1,0],[1,0]]:
      f(y+i,x+j,c0)
    return 1

while 1:
  H,W=map(int,raw_input().split())
  if H==W==0: break
  A=[["X" for _ in range(W+2)] for _ in range(H+2)]
  for i in range(H):
    s=raw_input()
    for j in range(W): A[i+1][j+1]=s[j]
  c=0
  for i in range(1,H+1):
    for j in range(1,W+1):
      c+=f(i,j,A[i][j])
  print c

Submission status: Wrong Answer

Solution code:
def f(y,x,c0):
  global A
  c=A[y][x]
  if c=="X" or c!=c0:
    return 0
  else:
    return 0
#    A[y][x]="X"
#    for i,j in [[0,-1],[0,1],[-1,0],[1,0]]:
#      f(y+i,x+j,c0)
#    return 1

while 1:
  H,W=map(int,raw_input().split())
  if H==W==0: break
  A=[["X" for _ in range(W+2)] for _ in range(H+2)]
  for i in range(H):
    s=raw_input()
    for j in range(W): A[i+1][j+1]=s[j]
  c=0
  for i in range(1,H+1):
    for j in range(1,W+1):
      c+=f(i,j,A[i][j])
  print c

Submission status: Wrong Answer

Solution code:
def f(y,x,c0):
  global A
  c=A[y][x]
  if c=="X" or c!=c0:
    return 0
  else:
    A[y][x]="X"
#    for i,j in [[0,-1],[0,1],[-1,0],[1,0]]:
#      f(y+i,x+j,c0)
    return 1

while 1:
  H,W=map(int,raw_input().split())
  if H==W==0: break
  A=[["X" for _ in range(W+2)] for _ in range(H+2)]
  for i in range(H):
    s=raw_input()
    for j in range(W): A[i+1][j+1]=s[j]
  c=0
  for i in range(1,H+1):
    for j in range(1,W+1):
      c+=f(i,j,A[i][j])
  print c

Submission status: Wrong Answer

Solution code:
def f(y,x,c0):
  global A
  c=A[y][x]
  if c=="X" or c!=c0:
    return 0
  else:
    A[y][x]="X"
    for i,j in [[0,-1],[0,1],[-1,0],[1,0]]:
      tmp=0
#      f(y+i,x+j,c0)
    return 1

while 1:
  H,W=map(int,raw_input().split())
  if H==W==0: break
  A=[["X" for _ in range(W+2)] for _ in range(H+2)]
  for i in range(H):
    s=raw_input()
    for j in range(W): A[i+1][j+1]=s[j]
  c=0
  for i in range(1,H+1):
    for j in range(1,W+1):
      c+=f(i,j,A[i][j])
  print c

Submission status: Wrong Answer

Solution code:
def f(y,x,c0):
  global A
  c=A[y][x]
  if c=='X' or c!=c0:
    return 0
  else:
    A[y][x]='X'
    for i,j in [[0,-1],[0,1],[-1,0],[1,0]]:
      tmp=1
#      f(y+i,x+j,c0)
    return 1

while 1:
  H,W=map(int,raw_input().split())
  if H==W==0: break
  A=[['X' for _ in range(W+2)] for _ in range(H+2)]
  for i in range(H):
    A[i+1][1:W+1]=list(raw_input())
  c=0
  for i in range(1,H+1):
    for j in range(1,W+1):
      c+=f(i,j,A[i][j])
  print c

Submission status: Wrong Answer

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

H, W = map(int, raw_input().split(' '))

print H, W

data = []
for i in range(H):
	data.append(list(raw_input()))

zeros = raw_input()

flg = [[0 for i in range(H)] for j in range(W)]


def search(i,j,mark):
	# if i<0 or i>=H or j<0 or j>=W:
	# 	break
	if i+1 < H and flg[i+1][j] != 1 and data[i+1][j] == mark:
		flg[i+1][j] = 1
		search(i+1,j,mark)
	if i-1 >= 0 and flg[i-1][j] != 1 and data[i-1][j] == mark:
		flg[i-1][j] = 1
		search(i-1,j,mark)
	if j+1 < W and flg[i][j+1] != 1 and data[i][j+1] == mark :
		flg[i][j+1] = 1
		search(i,j+1,mark)
	if j-1 >= 0 and flg[i][j-1] != 1 and data[i][j-1] == mark :
		flg[i][j-1] = 1
		search(i,j-1,mark)



count = 0
for i in range(H):
	for j in range(W):
		if flg[i][j] != 0:
			continue
		search(i,j,data[i][j])
		count += 1

print count
# print data
# print flg

Submission status: Wrong Answer

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

H, W = map(int, raw_input().split(' '))

#print H, W

data = []
for i in range(H):
	data.append(list(raw_input()))

zeros = raw_input()

flg = [[0 for i in range(H)] for j in range(W)]


def search(i,j,mark):
	# if i<0 or i>=H or j<0 or j>=W:
	# 	break
	if i+1 < H and flg[i+1][j] != 1 and data[i+1][j] == mark:
		flg[i+1][j] = 1
		search(i+1,j,mark)
	if i-1 >= 0 and flg[i-1][j] != 1 and data[i-1][j] == mark:
		flg[i-1][j] = 1
		search(i-1,j,mark)
	if j+1 < W and flg[i][j+1] != 1 and data[i][j+1] == mark :
		flg[i][j+1] = 1
		search(i,j+1,mark)
	if j-1 >= 0 and flg[i][j-1] != 1 and data[i][j-1] == mark :
		flg[i][j-1] = 1
		search(i,j-1,mark)



count = 0
for i in range(H):
	for j in range(W):
		if flg[i][j] != 0:
			continue
		search(i,j,data[i][j])
		count += 1

print count
# print data
# print flg

Submission status: Wrong Answer

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

H, W = map(int, raw_input().split(' '))

#print H, W

data = []
for i in range(H):
	data.append(list(raw_input()))

zeros = raw_input()

flg = [[0 for j in range(W)] for i in range(H)]


def search(i,j,mark):
	# if i<0 or i>=H or j<0 or j>=W:
	# 	break
	if i+1 < H and flg[i+1][j] == 0 and data[i+1][j] == mark:
		flg[i+1][j] = 1
		search(i+1,j,mark)
	if i-1 >= 0 and flg[i-1][j] == 0 and data[i-1][j] == mark:
		flg[i-1][j] = 1
		search(i-1,j,mark)
	if j+1 < W and flg[i][j+1] == 0 and data[i][j+1] == mark :
		flg[i][j+1] = 1
		search(i,j+1,mark)
	if j-1 >= 0 and flg[i][j-1] == 0 and data[i][j-1] == mark :
		flg[i][j-1] = 1
		search(i,j-1,mark)



count = 0
for i in range(H):
	for j in range(W):
		if flg[i][j] != 0:
			continue
		search(i,j,data[i][j])
		count += 1

print count
# print data
# print flg

Submission status: Wrong Answer

Solution code:
H, W = map(int, raw_input().split(' '))

#print H, W

data = []
for i in range(H):
	data.append(list(raw_input()))

zeros = raw_input()

flg = [[0 for j in range(W)] for i in range(H)]


def search(i,j,mark):
	# if i<0 or i>=H or j<0 or j>=W:
	# 	break
	if i+1 < H and flg[i+1][j] == 0 and data[i+1][j] == mark:
		flg[i+1][j] = 1
		search(i+1,j,mark)
	if i-1 >= 0 and flg[i-1][j] == 0 and data[i-1][j] == mark:
		flg[i-1][j] = 1
		search(i-1,j,mark)
	if j+1 < W and flg[i][j+1] == 0 and data[i][j+1] == mark :
		flg[i][j+1] = 1
		search(i,j+1,mark)
	if j-1 >= 0 and flg[i][j-1] == 0 and data[i][j-1] == mark :
		flg[i][j-1] = 1
		search(i,j-1,mark)



count = 0
for i in range(H):
	for j in range(W):
		if flg[i][j] != 0:
			continue
		search(i,j,data[i][j])
		count += 1

print count
# print data
# print flg

Submission status: Wrong Answer

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

h, w = list(map(lambda x: int(x),input().split(" ")))
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(list1)
print(n)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)

def check(y,x):
    if 0<=y<=h-1 and 0<=x<=w-1:
        return True
    return False

def dfs(y,x):
    dx=[1,0,-1,0]
    dy=[0,1,0,-1]
    color[y][x]=1
    for i in xrange(4):
        nextx=x+dx[i]
        nexty=y+dy[i]
        if not check(nexty,nextx):continue
        if g[nexty][nextx]==g[y][x] and color[nexty][nextx]==0:
            dfs(nexty,nextx)
    color[y][x]=2
    return 1

h,w=map(int,raw_input().split())
g=[raw_input() for _ in xrange(h)]
cnt=0
color=[[0]*w for _ in xrange(h)]
for i in xrange(h):
    for j in xrange(w):
        if color[i][j]==2:continue
        cnt+=dfs(i,j)
print(cnt)

Submission status: Wrong Answer

Solution code:

h, w = map(int, raw_input().split())

map = []
uni = set([])
for _ in range(h):
    s = raw_input()
    uni.update(list(s))
    map.append(list(s))

count = 0


def dfs(x, y, symbol):
    map[y][x] = "[]"

    move = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    for delta_x, delta_y in move:
        nx = x + delta_x
        ny = y + delta_y
        if 0 <= nx and nx < len(map[0]) and 0 <= ny and ny < len(map):
            if map[ny][nx] == symbol:
                dfs(nx, ny, symbol)

for idx in range(h):
    for jdx in range(w):
        if {map[idx][jdx]} & uni:
            count += 1
            dfs(jdx, idx, map[idx][jdx])

print count

Submission status: Wrong Answer

Solution code:

h, w = map(int, raw_input().split())

map = []
uni = set([])
for _ in range(h):
    s = raw_input()
    uni.update(list(s))
    map.append(list(s))
end = raw_input()
count = 0


def dfs(x, y, symbol):
    map[y][x] = "[]"

    move = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    for delta_x, delta_y in move:
        nx = x + delta_x
        ny = y + delta_y
        if 0 <= nx and nx < len(map[0]) and 0 <= ny and ny < len(map):
            if map[ny][nx] == symbol:
                dfs(nx, ny, symbol)

for idx in range(h):
    for jdx in range(w):
        if {map[idx][jdx]} & uni:
            count += 1
            dfs(jdx, idx, map[idx][jdx])

print count

Submission status: Wrong Answer

Solution code:
def search(values,hp=0,vp=0,stamp=0,fruit=-1):
	if not (0<=hp<len(values)): return stamp
	if not (0<=vp<len(values[0])): return stamp
	if fruit!=-1 and fruit!=values[hp][vp]: return stamp
	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()])
#for v in values:
#	print(v)
print(solve(values))

Submission status: Wrong Answer

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

def search(values,hp=0,vp=0,stamp=0,fruit=-1):
	if not (0<=hp<len(values)): return stamp
	if not (0<=vp<len(values[0])): return stamp
	if fruit!=-1 and fruit!=values[hp][vp]: return stamp
	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: Wrong Answer

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

def search(values,hp,vp,stamp,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

if __name__=='__main__':
	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: Wrong Answer

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

def search(values,hp,vp,stamp,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

if __name__=='__main__':
    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: Wrong Answer

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

def search(values,hp,vp,stamp,fruit):
    if not (0<=hp<len(values[0])): return
    if not (0<=vp<len(values)): 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

if __name__=='__main__':
    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: Wrong Answer

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
    fruit,values[hp][vp]=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__':
    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: Wrong Answer

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

def search(values,hp,vp,stamp,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,values[i][j])
                stamp+=1
    return stamp

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

Submission status: Wrong Answer

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

def search(values,hp,vp,stamp,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,values[i][j])
                stamp+=1
    return stamp

if __name__=='__main__':
    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: Wrong Answer

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

def search(values,hp,vp,stamp,fruit):
    if not (0<=hp<len(values)): return
    if not (0<=vp<len(values[hp])): 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,values[i][j])
                stamp+=1
    return stamp

if __name__=='__main__':
    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: Wrong Answer

Solution code:
import sys


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()]


if __name__ == '__main__':
    process()

Submission status: Wrong Answer

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

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: Wrong Answer

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

def solve(values):
	count=0
	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: Wrong Answer

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
	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: Wrong Answer

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: Wrong Answer

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: Wrong Answer

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: Wrong Answer

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: Wrong Answer

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

Submission status: Wrong Answer

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]=True
	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: Wrong Answer

Solution code:
if __name__ == "__main__":
    dataset = [line.strip() for line in raw_input()]
    print dataset

Submission status: Wrong Answer

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

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

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().strip(),list()
	while line!='0 0':
		H,W = list(map(int,line.split(' ')))
		value = list()
		for _ in range(H):
			value.append(list(x for x in input().strip()))
		values.append(value)
		line = input().strip()
	for value in values:
		print(solve(value))

if __name__=='__main__':
	main()

Submission status: Wrong Answer

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

import sys
sys.setrecursionlimit(10000000)

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

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().strip(),list()
	H,W = list(map(int,line.split(' ')))
	values = list()
	for _ in range(H):
		values.append(list(x for x in input().strip()))
	print(solve(values))

if __name__=='__main__':
	main()