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:
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: Runtime Error

Solution code:
def solve(c,h,w,count):
	if 0 <= h < H and 0 <= w < W:
		if type(A[h][w]) is not int and c == A[h][w]:
			A[h][w] = count
			for dh,dw in [[1,0],[-1,0],[0,1],[0,-1]]:
				solve(c,h+dh,w+dw,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: Runtime Error

Solution code:
def solve(c,h,w):
	if A[h][w] == c:
		A[h][w] = "X"
		for dx,dy in zip((1,-1,0,0),(0,0,1,-1)):
			if 0 <= h+dh < H and 0 <= w+dw < W:
				solve(c,h+dh,w+dw)

while 1:
	H,W = map(int,raw_input().split())
	if H == W == 0: break
	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: Runtime Error

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 i in range(H+2):
    if i==0 or i==H+1:
      A.append(list(" "*(W+2)))
    else:
      A.append(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: Runtime Error

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]=s[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: Runtime Error

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().strip()+" "
      for j in range(W+2): A[i][j]=s[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: Runtime Error

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(1,W+1): A[i][j]=s[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: Runtime Error

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().rstrip()
      for j in range(1,W+1): A[i][j]=s[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: Runtime Error

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]=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: Runtime Error

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): A[i][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: Runtime Error

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):
    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: Runtime Error

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: Runtime Error

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+=A[i][j]
#      c+=f(i,j,A[i][j])
  print c

Submission status: Runtime Error

Solution code:
def f(y,x,c0):
  global A
  c=A[y][x]
  if x==0 or x==W+1 or y==0 or y==H+1: 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: Runtime Error

Solution code:
  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: Runtime Error

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):
    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: Runtime Error

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

Submission status: Runtime Error

Solution code:
def dfs(x,y,ch):
	field[x][y] = '.'
	for i in range(4):
			nx = x+moveY[i]
			ny = y+moveX[i]
			if (nx >= 0 and ny >= 0) and(nx < H and ny < W) and field[nx][ny] == ch:
				if field[nx][ny] == '#' or field[nx][ny] == '*'or field[nx][ny] == '@':
					dfs(nx,ny,field[nx][ny])
if __name__ == '__main__':
	moveY = [-1,0,1,0]
	moveX = [0,1,0,-1]
	H,W = map(int,raw_input().split(' '))
	field = ['' for i in range(H)]
	h = 0
	while True:
		s = raw_input()
		if s == '0 0':
			break
		field[h] = list(s)
		h += 1
	correct =0
	for i in range(H):
		for j in range(W):
			if field[i][j] == '@' or field[i][j] == '#'or field[i][j] == '*':
				dfs(i,j,field[i][j])
				correct += 1
	print correct

Submission status: Runtime Error

Solution code:
def dfs(x,y,ch):
	field[x][y] = '.'
	for i in range(4):
			nx = x+moveY[i]
			ny = y+moveX[i]
			if (nx >= 0 and ny >= 0) and(nx < H and ny < W) and field[nx][ny] == ch:
				if field[nx][ny] == '#' or field[nx][ny] == '*'or field[nx][ny] == '@':
					dfs(nx,ny,field[nx][ny])
moveY = [-1,0,1,0]
moveX = [0,1,0,-1]
H,W = map(int,raw_input().split(' '))
field = ['' for i in range(H)]
h = 0
while True:
	s = raw_input()
	if s == '0 0':
		break
	field[h] = list(s)
	h += 1
correct =0
for i in range(H):
	for j in range(W):
		if field[i][j] == '@' or field[i][j] == '#'or field[i][j] == '*':
			dfs(i,j,field[i][j])
			correct += 1
print correct

Submission status: Runtime Error

Solution code:
def dfs(X,Y,ch):
	stack.append([X,Y])
	field[X][Y] = '.'
	while len(stack) != 0:
		x,y = stack.pop()
		for i in range(4):
				nx = x+moveY[i]
				ny = y+moveX[i]
				if (nx >= 0 and ny >= 0) and(nx < H and ny < W) and field[nx][ny] == ch:
					if field[nx][ny] == '#' or field[nx][ny] == '*'or field[nx][ny] == '@':
						field[nx][ny] = '.'
						stack.append([nx,ny])
stack = []
moveY = [-1,0,1,0]
moveX = [0,1,0,-1]
H,W = map(int,raw_input().split(' '))
field = ['' for i in range(H)]
h = 0
while True:
	s = raw_input()
	if s == '0 0':
		break
	field[h] = list(s)
	h += 1
correct =0
for i in range(H):
	for j in range(W):
		if field[i][j] == '@' or field[i][j] == '#'or field[i][j] == '*':
			dfs(i,j,field[i][j])
			correct += 1
print correct

Submission status: Runtime Error

Solution code:
def dfs(X,Y,ch):
	stack.append([X,Y])
	print stack
	field[X][Y] = '.'
	while len(stack) != 0:
		x,y = stack.pop()
		for i in range(4):
				nx = x+moveY[i]
				ny = y+moveX[i]
				if (nx >= 0 and ny >= 0) and(nx < H and ny < W) and field[nx][ny] == ch:
					if field[nx][ny] == '#' or field[nx][ny] == '*'or field[nx][ny] == '@':
						field[nx][ny] = '.'
						stack.append([nx,ny])
						print stack
stack = []
moveY = [-1,0,1,0]
moveX = [0,1,0,-1]
while True:
	H,W = map(int,raw_input().split(' '))
	if H == 0 and W == 0:
		break
	field = ['' for i in range(H)]
	h = 0
	while True:
		field[h] = list(raw_input())
		h += 1
	correct =0
	for i in range(H):
		for j in range(W):
			if field[i][j] == '@' or field[i][j] == '#'or field[i][j] == '*':
				dfs(i,j,field[i][j])
				correct += 1
	print correct

Submission status: Runtime Error

Solution code:
def dfs(X,Y,ch):
	field[X][Y] = '.'
	for i in range(4):
		nx = X+moveY[i]
		ny = Y+moveX[i]
		if (nx >= 0 and ny >= 0) and(nx < H and ny < W) and field[nx][ny] == ch:
			if field[nx][ny] == '#' or field[nx][ny] == '*'or field[nx][ny] == '@':
				dfs(nx,ny,field[nx][ny])
moveY = [-1,0,1,0]
moveX = [0,1,0,-1]
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)]
	print field
	correct =0
	for i in range(H):
		for j in range(W):
			if field[i][j] == '@' or field[i][j] == '#'or field[i][j] == '*':
				dfs(i,j,field[i][j])
				correct += 1
	print correct

Submission status: Runtime Error

Solution code:
def wa(h, w, f):
    if not (0 <= h <= H-1 and 0 <= w <= W-1):
        return
    elif mapp[h][w] != f:
        return
    else:
        mapp[h][w] = '$'
        #right
        wa(h, w+1, f)
        #up
        wa(h-1, w, f)
        #left
        wa(h, w-1, f)
        #down
        wa(h+1, w, f)

while True:
    H, W = map(int, raw_input().split(' '))
    if H == 0: break
    mapp = [list(raw_input()) for i in range(H)]
    count = 0
    for h in range(H):
        for w in range(W):
            if mapp[h][w] == '@':
                wa(h, w, '@')
            elif mapp[h][w] == '#':
                wa(h, w, '#')
            elif mapp[h][w] == '*':
                wa(h, w, '*')
            else:
                continue
            count += 1
    else:
        print count

Submission status: Runtime Error

Solution code:
def wa(h, w, f):
    if not (0 <= h <= H-1 and 0 <= w <= W-1):
        return
    elif mapp[h][w] != f:
        return
    else:
        mapp[h][w] = '$'
        wa(h, w+1, f)
        wa(h-1, w, f)
        wa(h, w-1, f)
        wa(h+1, w, f)

while True:
    H, W = map(int, raw_input().split(' '))
    if H == 0: break
    mapp = [list(raw_input()) for i in range(H)]
    count = 0
    for h in range(H):
        for w in range(W):
            if mapp[h][w] == '@':
                wa(h, w, '@')
            elif mapp[h][w] == '#':
                wa(h, w, '#')
            elif mapp[h][w] == '*':
                wa(h, w, '*')
            else:
                continue
            count += 1
    else:
        print count

Submission status: Runtime Error

Solution code:
def wa(h, w, f):
    if not (0 <= h <= H-1 and 0 <= w <= W-1):
        return
    elif mapp[h][w] != f:
        return
    else:
        mapp[h][w] = '$'
        wa(h, w+1, f)
        wa(h-1, w, f)
        wa(h, w-1, f)
        wa(h+1, w, f)

while True:
    H, W = map(int, raw_input().split(' '))
    if H == 0 and W == 0: break
    mapp = [list(raw_input()) for i in range(H)]
    count = 0
    for h in range(H):
        for w in range(W):
            if mapp[h][w] == '@':
                wa(h, w, '@')
            elif mapp[h][w] == '#':
                wa(h, w, '#')
            elif mapp[h][w] == '*':
                wa(h, w, '*')
            else:
                continue
            count += 1
    else:
        print count

Submission status: Runtime Error

Solution code:
def wa(h, w, f):
    if not (0 <= h <= H-1 and 0 <= w <= W-1):
        return
    elif mapp[h][w] != f:
        return
    else:
        mapp[h][w] = '$'
        wa(h, w+1, f)
        wa(h-1, w, f)
        wa(h, w-1, f)
        wa(h+1, w, f)

while True:
    H, W = map(int, raw_input().split(' '))
    if H == 0 and W == 0: break
    mapp = []
    for i in range(H):
        mapp.append(list(raw_input()))
    count = 0
    for h in range(H):
        for w in range(W):
            if mapp[h][w] == '@':
                wa(h, w, '@')
            elif mapp[h][w] == '#':
                wa(h, w, '#')
            elif mapp[h][w] == '*':
                wa(h, w, '*')
            else:
                continue
            count += 1
    else:
        print count

Submission status: Runtime Error

Solution code:
def wa(h, w, f):
    if not (0 <= h <= H-1 and 0 <= w <= W-1):
        return
    elif mapp[h][w] != f:
        return
    else:
        mapp[h][w] = '$'
        wa(h, w+1, f)
        wa(h-1, w, f)
        wa(h, w-1, f)
        wa(h+1, w, f)

while True:
    H, W = map(int, raw_input().split(' '))
    if H == 0 and W == 0: break
    mapp = [list(raw_input()[i*W:i*W+W]) for i in range(H)]
    count = 0
    for h in range(H):
        for w in range(W):
            if mapp[h][w] == '@':
                wa(h, w, '@')
            elif mapp[h][w] == '#':
                wa(h, w, '#')
            elif mapp[h][w] == '*':
                wa(h, w, '*')
            else:
                continue
            count += 1
    else:
        print count

Submission status: Runtime Error

Solution code:
def wa(h, w, f):
    if not (0 <= h <= H-1 and 0 <= w <= W-1):
        return
    elif mapp[h][w] != f:
        return
    else:
        mapp[h][w] = '$'
        wa(h, w+1, f)
        wa(h-1, w, f)
        wa(h, w-1, f)
        wa(h+1, w, f)

while True:
    H, W = map(int, raw_input().split(' '))
    if H == 0 and W == 0: break
    line = raw_input()
    mapp = [list(line[i*W:i*W+W]) for i in range(H)]
    for line in mapp:
        print line
    count = 0
    for h in range(H):
        for w in range(W):
            if mapp[h][w] == '@':
                wa(h, w, '@')
            elif mapp[h][w] == '#':
                wa(h, w, '#')
            elif mapp[h][w] == '*':
                wa(h, w, '*')
            else:
                continue
            count += 1
    else:
        print count

Submission status: Runtime Error

Solution code:
def wa(h, w, f):
    if not (0 <= h <= H-1 and 0 <= w <= W-1):
        return
    elif mapp[h][w] != f:
        return
    else:
        mapp[h][w] = '$'
        wa(h, w+1, f)
        wa(h-1, w, f)
        wa(h, w-1, f)
        wa(h+1, w, f)
        
while True:
    H, W = map(int, raw_input().split(' '))
    if H == 0 and W == 0: break
    line = raw_input()
    mapp = [list(line[i*W:i*W+W]) for i in range(H)]
    count = 0
    for h in range(H):
        for w in range(W):
            if mapp[h][w] == '@':
                wa(h, w, '@')
            elif mapp[h][w] == '#':
                wa(h, w, '#')
            elif mapp[h][w] == '*':
                wa(h, w, '*')
            else:
                continue
            count += 1
    else:
        print count

Submission status: Runtime Error

Solution code:
def wa(h, w, f):
    if not (0 <= h <= H-1 and 0 <= w <= W-1):
        return
    elif mapp[h][w] != f:
        return
    else:
        mapp[h][w] = '$'
        wa(h, w+1, f)
        wa(h-1, w, f)
        wa(h, w-1, f)
        wa(h+1, w, f)

while True:
    H, W = map(int, raw_input().split(' '))
    if H == 0 and W == 0: break
    mapp = [list(raw_input()) for i in range(H)]
    count = 0
    for h in range(H):
        for w in range(W):
            if mapp[h][w] == '@':
                count += 1
                wa(h, w, '@')
            elif mapp[h][w] == '#':
                count += 1
                wa(h, w, '#')
            elif mapp[h][w] == '*':
                count += 1
                wa(h, w, '*')
    else:
        print count

Submission status: Runtime Error

Solution code:
def wa(h, w, f):
    if not (0 <= h <= H-1 and 0 <= w <= W-1):
        return
    elif mapp[h][w] != f:
        return
    else:
        mapp[h][w] = '$'
        wa(h, w+1, f)
        wa(h-1, w, f)
        wa(h, w-1, f)
        wa(h+1, w, f)

while True:
    H, W = map(int, raw_input().split())
    if H == 0 and W == 0: break
    mapp = [list(raw_input()) for i in range(H)]
    count = 0
    for h in range(H):
        for w in range(W):
            if mapp[h][w] == '@':
                count += 1
                wa(h, w, '@')
            elif mapp[h][w] == '#':
                count += 1
                wa(h, w, '#')
            elif mapp[h][w] == '*':
                count += 1
                wa(h, w, '*')
    else:
        print count

Submission status: Runtime Error

Solution code:
def wa(h, w, f):
    if not (0 <= h < H and 0 <= w < W):
        return
    elif mapp[h][w] != f:
        return
    else:
        mapp[h][w] = '$'
        wa(h, w+1, f)
        wa(h-1, w, f)
        wa(h, w-1, f)
        wa(h+1, w, f)

while True:
    H, W = map(int, raw_input().split())
    if H == 0 and W == 0: break
    mapp = [list(raw_input()) for i in range(H)]
    count = 0
    for h in range(H):
        for w in range(W):
            if mapp[h][w] == '@':
                count += 1
                wa(h, w, '@')
            elif mapp[h][w] == '#':
                count += 1
                wa(h, w, '#')
            elif mapp[h][w] == '*':
                count += 1
                wa(h, w, '*')
    else:
        print count

Submission status: Runtime Error

Solution code:
def wa(h, w, f):
    if not (0 <= h < H and 0 <= w < W):
        return
    elif mapp[h][w] != f:
        return
    else:
        mapp[h][w] = '$'
        wa(h, w+1, f)
        wa(h-1, w, f)
        wa(h, w-1, f)
        wa(h+1, w, f)

while True:
    W, H = map(int, raw_input().split())
    if H == 0 and W == 0: break
    mapp = [list(raw_input()) for i in range(H)]
    count = 0
    for h in range(H):
        for w in range(W):
            if mapp[h][w] == '@':
                count += 1
                wa(h, w, '@')
            elif mapp[h][w] == '#':
                count += 1
                wa(h, w, '#')
            elif mapp[h][w] == '*':
                count += 1
                wa(h, w, '*')
    else:
        print count

Submission status: Runtime Error

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

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

	if H==0 and W==0:
		break
	#print H, W

	data = []
	for i in range(H):
		data.append(list(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: Runtime Error

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

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


	#print H, W

	data = []
	for i in range(H):
		data.append(list(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
	if H==0 and W==0:
		break
	else:
		print count
# print data
# print flg

Submission status: Runtime Error

Solution code:
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def dfs(i, j):
    if (area[i][j] == '#'):
        area[i][j] = "."

        for k in range(4):
            nx = i + dx[k]
            ny = j + dy[k]
            
            if ((0 <= nx < W) and (0 <= ny < H) and (area[nx][ny] == '#')):
                dfs(nx, ny)

    elif (area[i][j] == '@'):
        area[i][j] = "."

        for k in range(4):
            nx = i + dx[k]
            ny = j + dy[k]
            
            if (0 <= nx < W and 0 <= ny < H and area[nx][ny] == '@'):
                dfs(nx, ny)

    elif (area[i][j] == '*'):
        area[i][j] = "."
        
        for k in range(4):
            nx = i + dx[k]
            ny = j + dy[k]
            
            if (0 <= nx < W and 0 <= ny < H and area[nx][ny] == '*'):
                dfs(nx, ny)
    

while True:
    H, W = map(int, raw_input().split())
    area = [list(raw_input()) for i in range(H)]
    
    if H == 0 and W == 0:
        break
    count = 0
    for i in range(W):
        for j in range(H):
            if area[i][j] == '#':
                dfs(i, j)
                count += 1
            if area[i][j] == '@':
                dfs(i, j)
                count += 1
            if area[i][j] == '*':
                dfs(i, j)
                count += 1
            else:
                continue

    print count

Submission status: Runtime Error

Solution code:
def check(x, y, flag):
	farm[x][y] = "$"
	if farm[x + 1][y] != "$" and farm[x + 1][y] == flag : check(x + 1, y, flag)
	if farm[x - 1][y] != "$" and farm[x - 1][y] == flag : check(x - 1, y, flag)
	if farm[x][y + 1] != "$" and farm[x][y + 1] == flag : check(x, y + 1, flag)
	if farm[x][y - 1] != "$" and farm[x][y - 1] == flag : check(x, y - 1, flag)

while True:
	h,w = map(int, raw_input().split())
	if h == 0 and w == 0 : break

	farm = []
	farm.append(list("$" * (w + 2)))
	for i in range(0, h):
		farm.append(list("$" + raw_input() + "$"))
	farm.append(list("$" * (w + 2)))
	
	count = 0
	for i in range(0, h):
		for j in range(0, w):
			if farm[i + 1][j + 1] != "$":
				check(i + 1, j + 1, farm[i + 1][j + 1])
				count += 1
				
	print count

Submission status: Runtime Error

Solution code:
def check(x, y, flag):
	farm[x][y] = "$"
	if farm[x + 1][y] == flag : check(x + 1, y, flag)
	if farm[x - 1][y] == flag : check(x - 1, y, flag)
	if farm[x][y + 1] == flag : check(x, y + 1, flag)
	if farm[x][y - 1] == flag : check(x, y - 1, flag)

while True:
	h,w = map(int, raw_input().split())
	if h == 0 and w == 0 : break

	farm = []
	farm.append(list("$" * (w + 2)))
	for i in range(0, h):
		farm.append(list("$" + raw_input() + "$"))
	farm.append(list("$" * (w + 2)))
	
	count = 0
	for i in range(0, h):
		for j in range(0, w):
			if farm[i + 1][j + 1] != "$":
				check(i + 1, j + 1, farm[i + 1][j + 1])
				count += 1
				
	print count

Submission status: Runtime Error

Solution code:
def check(x, y, flag):
	farm[x][y] = "$"
	if farm[x + 1][y] == flag : check(x + 1, y, flag)
	if farm[x - 1][y] == flag : check(x - 1, y, flag)
	if farm[x][y + 1] == flag : check(x, y + 1, flag)
	if farm[x][y - 1] == flag : check(x, y - 1, flag)

while True:
	h,w = map(int, raw_input().split())
	if h == 0 and w == 0 : break

	farm = []
	farm.append(list("$" * (h + 2)))
	for i in range(0, w):
		farm.append(list("$" + raw_input() + "$"))
	farm.append(list("$" * (h + 2)))
	
	count = 0
	for i in range(0, w):
		for j in range(0, h):
			if farm[i + 1][j + 1] != "$":
				check(i + 1, j + 1, farm[i + 1][j + 1])
				count += 1
				
	print count

Submission status: Runtime Error

Solution code:
def check(x, y, flag):
	farm[x][y] = "$"
	if farm[x + 1][y] == flag : check(x + 1, y, flag)
	if farm[x - 1][y] == flag : check(x - 1, y, flag)
	if farm[x][y + 1] == flag : check(x, y + 1, flag)
	if farm[x][y - 1] == flag : check(x, y - 1, flag)
	return 1

while True:
	h,w = map(int, raw_input().split())
	if h == 0 and w == 0 : break

	farm = []
	farm.append(list("$" * (w + 2)))
	for i in range(0, h):
		farm.append(list("$" + raw_input() + "$"))
	farm.append(list("$" * (w + 2)))
	
	count = 0
	for i in range(0, h):
		for j in range(0, w):
			if farm[i + 1][j + 1] != "$":
				count += check(i + 1, j + 1, farm[i + 1][j + 1])
				
	print count

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)

def check(x, y, flag):
	farm[x][y] = "$"
	if farm[x + 1][y] == flag : check(x + 1, y, flag)
	if farm[x - 1][y] == flag : check(x - 1, y, flag)
	if farm[x][y + 1] == flag : check(x, y + 1, flag)
	if farm[x][y - 1] == flag : check(x, y - 1, flag)
	return 1

while True:
	h,w = map(int, raw_input().split())
	if h == 0 and w == 0 : break

	farm = []
	farm.append(list("$" * (w + 2)))
	for i in range(0, h):
		farm.append(list("$" + raw_input() + "$"))
	farm.append(list("$" * (w + 2)))
	
	count = 0
	for i in range(0, h):
		for j in range(0, w):
			if farm[i + 1][j + 1] != "$":
				count += check(i + 1, j + 1, farm[i + 1][j + 1])
				
	print count

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)

Submission status: Runtime Error

Solution code:
h = w = 0
slist =[]
list1=[]
def dfs(f):
    while True:
        if len(slist) == 0:
            return 1
        coordinate= slist.pop()
        #print(coordinate)
        x = coordinate[0]
        y = coordinate[1]

        list1[y] = list1[y][:x] + '1' + list1[y][x + 1:]
        if x + 1 < w:
            if list1[y][x + 1] == f:
                slist.append([x + 1,y])
        if x - 1 >= 0:
            if list1[y][x-1] == f:
                slist.append([x - 1, y])
        if y + 1 < h:
            if list1[y + 1][x] == f:
                slist.append([x, y + 1])
        if y -1 >= 0:
            if list1[y -1][x] == f:
                slist.append([x, y - 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':
                slist.append([j,i])
                n += dfs(list1[i][j])
            #print(list1)
    print(n)