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:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0118
"""
import sys



def solve(data):
    size = len(data[0])
    data.insert(0, ['0' for _ in range(size)])
    data.insert(len(data), ['0' for _ in range(size)])

    processing_area = []
    area_id = 0
    for y in range(1, len(data)):
        for x in range(1, len(data[0])):
            print('y:{}, x:{}'.format(y, x))
            mark = data[y][x]
            data[y][x] = '0'
            if mark != '0':
                area_id += 1
                processing_area.append((y, x))

                while processing_area:
                    yy, xx = processing_area.pop()
                    if data[yy-1][xx] == mark:
                        data[yy-1][xx] = '0'
                        processing_area.append((yy-1, xx))
                    if data[yy+1][xx] == mark:
                        data[yy+1][xx] = '0'
                        processing_area.append((yy+1, xx))
                    if data[yy][xx-1] == mark:
                        data[yy][xx-1] = '0'
                        processing_area.append((yy, xx-1))
                    if data[yy][xx+1] == mark:
                        data[yy][xx+1] = '0'
                        processing_area.append((yy, xx+1))
                mark = '0'
    return area_id



def main(args):
    # data = []
    # data.append(['0', '#', '#', '#', '#', '*', '*', '*', '*', '*', '@', '0'])
    # data.append(['0', '@', '#', '@', '@', '@', '@', '#', '*', '#', '*', '0'])
    # data.append(['0', '@', '#', '#', '*', '*', '*', '@', '@', '@', '*', '0'])
    # data.append(['0', '#', '*', '*', '*', '*', '#', '*', '@', '*', '*', '0'])
    # data.append(['0', '#', '#', '@', '*', '#', '@', '@', '*', '#', '#', '0'])
    # data.append(['0', '*', '@', '@', '@', '@', '*', '@', '@', '@', '#', '0'])
    # data.append(['0', '*', '*', '*', '#', '@', '*', '@', '#', '#', '*', '0'])
    # data.append(['0', '*', '@', '@', '@', '*', '@', '@', '#', '#', '@', '0'])
    # data.append(['0', '*', '@', '*', '#', '*', '@', '#', '#', '*', '*', '0'])
    # data.append(['0', '@', '*', '*', '*', '*', '#', '@', '@', '#', '@', '0'])

    while True:
        h, w = [int(x) for x in input().split(' ')]
        if h == 0 and w == 0:
            break
        data = []
        for _ in range(w):
            temp = list(input())
            temp.insert(0, '0')
            temp.append('0')
            data.append(temp)

        result = solve(data)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0118
"""
import sys



def solve(data):
    size = len(data[0])
    data.insert(0, ['0' for _ in range(size)])
    data.insert(len(data), ['0' for _ in range(size)])

    processing_area = []
    area_id = 0
    for y in range(1, len(data)):
        for x in range(1, len(data[0])):
            # print('y:{}, x:{}'.format(y, x))
            mark = data[y][x]
            data[y][x] = '0'
            if mark != '0':
                area_id += 1
                processing_area.append((y, x))

                while processing_area:
                    yy, xx = processing_area.pop()
                    if data[yy-1][xx] == mark:
                        data[yy-1][xx] = '0'
                        processing_area.append((yy-1, xx))
                    if data[yy+1][xx] == mark:
                        data[yy+1][xx] = '0'
                        processing_area.append((yy+1, xx))
                    if data[yy][xx-1] == mark:
                        data[yy][xx-1] = '0'
                        processing_area.append((yy, xx-1))
                    if data[yy][xx+1] == mark:
                        data[yy][xx+1] = '0'
                        processing_area.append((yy, xx+1))
                mark = '0'
    return area_id



def main(args):
    # data = []
    # data.append(['0', '#', '#', '#', '#', '*', '*', '*', '*', '*', '@', '0'])
    # data.append(['0', '@', '#', '@', '@', '@', '@', '#', '*', '#', '*', '0'])
    # data.append(['0', '@', '#', '#', '*', '*', '*', '@', '@', '@', '*', '0'])
    # data.append(['0', '#', '*', '*', '*', '*', '#', '*', '@', '*', '*', '0'])
    # data.append(['0', '#', '#', '@', '*', '#', '@', '@', '*', '#', '#', '0'])
    # data.append(['0', '*', '@', '@', '@', '@', '*', '@', '@', '@', '#', '0'])
    # data.append(['0', '*', '*', '*', '#', '@', '*', '@', '#', '#', '*', '0'])
    # data.append(['0', '*', '@', '@', '@', '*', '@', '@', '#', '#', '@', '0'])
    # data.append(['0', '*', '@', '*', '#', '*', '@', '#', '#', '*', '*', '0'])
    # data.append(['0', '@', '*', '*', '*', '*', '#', '@', '@', '#', '@', '0'])

    while True:
        h, w = [int(x) for x in input().split(' ')]
        if h == 0 and w == 0:
            break
        data = []
        for _ in range(w):
            temp = list(input())
            temp.insert(0, '0')
            temp.append('0')
            data.append(temp)

        result = solve(data)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0118
"""
import sys



def solve(data):
    size = len(data[0])
    data.insert(0, ['0' for _ in range(size)])
    data.insert(len(data), ['0' for _ in range(size)])

    processing_area = []
    area_id = 0
    for y in range(1, len(data)-1):
        for x in range(1, len(data[0])):
            # print('y:{}, x:{}'.format(y, x))
            mark = data[y][x]
            data[y][x] = '0'
            if mark != '0':
                area_id += 1
                processing_area.append((y, x))

                while processing_area:
                    yy, xx = processing_area.pop()
                    if data[yy-1][xx] == mark: #  ???
                        data[yy-1][xx] = '0'
                        processing_area.append((yy-1l, xx))
                    if data[yy+1][xx] == mark: #  ???
                        data[yy+1][xx] = '0'
                        processing_area.append((yy+1, xx))
                    if data[yy][xx-1] == mark: #  ???
                        data[yy][xx-1] = '0'
                        processing_area.append((yy, xx-1))
                    if data[yy][xx+1] == mark: #  ???
                        data[yy][xx+1] = '0'
                        processing_area.append((yy, xx+1))
                mark = '0'
    return area_id



def main(args):
    # data = []
    # data.append(['0', '#', '#', '#', '#', '*', '*', '*', '*', '*', '@', '0'])
    # data.append(['0', '@', '#', '@', '@', '@', '@', '#', '*', '#', '*', '0'])
    # data.append(['0', '@', '#', '#', '*', '*', '*', '@', '@', '@', '*', '0'])
    # data.append(['0', '#', '*', '*', '*', '*', '#', '*', '@', '*', '*', '0'])
    # data.append(['0', '#', '#', '@', '*', '#', '@', '@', '*', '#', '#', '0'])
    # data.append(['0', '*', '@', '@', '@', '@', '*', '@', '@', '@', '#', '0'])
    # data.append(['0', '*', '*', '*', '#', '@', '*', '@', '#', '#', '*', '0'])
    # data.append(['0', '*', '@', '@', '@', '*', '@', '@', '#', '#', '@', '0'])
    # data.append(['0', '*', '@', '*', '#', '*', '@', '#', '#', '*', '*', '0'])
    # data.append(['0', '@', '*', '*', '*', '*', '#', '@', '@', '#', '@', '0'])

    while True:
        h, w = [int(x) for x in input().split(' ')]
        if h == 0 and w == 0:
            break
        data = []
        for _ in range(h):
            temp = list(input())
            temp.insert(0, '0')
            temp.append('0')
            data.append(temp)

        result = solve(data)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Runtime Error

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


def main():
    global field, w, h
    while True:
        h, w = map(int, input().split())
        if h == 0 and w == 0:
            break
        count = 0
        field = [[0] * w for _ in range(h)]
        for y in range(h):
            kinds = [_ for _ in input()]
            arrangement(kinds, y)

        for y in range(h):
            for x in range(w):
                if field[y][x] != -1:
                    dfs(field[y][x], x, y)
                    count += 1

        print(count)


def arrangement(kinds, y):
    global field
    for i, kind in enumerate(kinds):
        field[y][i] = kind


def dfs(kind, x, y):
    global field
    field[y][x] = -1
    for dx, dy in zip([0, 0, -1, 1], [1, -1, 0, 0]):
        next_x = x + dx
        next_y = y + dy
        if next_x < 0 or next_y < 0 or next_x >= w or next_y >= h:
            continue
        if field[next_y][next_x] != kind:
            continue
        dfs(field[next_y][next_x], next_x, next_y)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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


def main():
    global field, w, h
    while True:
        h, w = map(int, input().split())
        if h == 0 and w == 0:
            break
        count = 0
        [field.append([_ for _ in input()]) for _ in range(h)]
        print(field)
        for y in range(h):
            for x in range(w):
                if field[y][x] != -1:
                    dfs(field[y][x], x, y)
                    count += 1
        print(count)


def arrangement(kinds, y):
    global field
    for i, kind in enumerate(kinds):
        field[y][i] = kind


def dfs(kind, x, y):
    global field
    print(x, y, field[y][x])
    field[y][x] = -1
    for dx, dy in zip([0, 0, -1, 1], [1, -1, 0, 0]):
        next_x = x + dx
        next_y = y + dy
        if next_x < 0 or next_y < 0 or next_x >= w or next_y >= h:
            continue
        if field[next_y][next_x] != kind:
            continue

        dfs(field[next_y][next_x], next_x, next_y)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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


def main():
    global field, w, h
    while True:
        h, w = map(int, input().split())
        if h == 0 and w == 0:
            break
        count = 0
        [field.append([_ for _ in input()]) for _ in range(h)]
        for y in range(h):
            for x in range(w):
                if field[y][x] != -1:
                    dfs(field[y][x], x, y)
                    count += 1
        print(count)


def dfs(kind, x, y):
    global field
    field[y][x] = -1
    for dx, dy in zip([0, 0, -1, 1], [1, -1, 0, 0]):
        next_x = x + dx
        next_y = y + dy
        if next_x < 0 or next_y < 0 or next_x >= w or next_y >= h:
            continue
        if field[next_y][next_x] != kind:
            continue
        dfs(field[next_y][next_x], next_x, next_y)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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


def main():
    global field, w, h
    while True:
        h, w = map(int, input().split())
        if h == 0 and w == 0:
            break
        count = 0
        field = []
        [field.append([_ for _ in input()]) for _ in range(h)]
        for y in range(h):
            for x in range(w):
                if field[y][x] != -1:
                    dfs(field[y][x], x, y)
                    count += 1
        print(count)


def dfs(kind, x, y):
    global field
    field[y][x] = -1
    for dx, dy in zip([0, 0, -1, 1], [1, -1, 0, 0]):
        next_x = x + dx
        next_y = y + dy
        if next_x < 0 or next_y < 0 or next_x >= w or next_y >= h:
            continue
        if field[next_y][next_x] != kind:
            continue
        dfs(field[next_y][next_x], next_x, next_y)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys

d = [(1, 0), (-1, 0), (0, 1), (0, -1)]


def dfs(x, y, c):
    judged[x][y] = True
    for dx, dy in d:
        if 0 <= x + dx < H and 0 <= y + dy < W and (not judged[x + dx][y + dy]) and orchard[x + dx][y + dy] == c:
            dfs(x + dx, y + dy, c)


if __name__ == '__main__':
    while True:
        H, W = list(map(int, input().split()))
        if H == 0 and W == 0:
            break
        orchard = [input() for i in range(H)]
        judged = [[False for i in range(W)] for j in range(H)]
        # print(orchard)
        ans = 0
        for i in range(0, H):
            for j in range(0, W):
                if not judged[i][j]:
                    ans += 1
                    dfs(i, j, orchard[i][j])

        print(ans)

Submission status: Runtime Error

Solution code:
import sys

d = [(1, 0), (-1, 0), (0, 1), (0, -1)]


def dfs(x, y, c):
    judged[x][y] = True
    for dx, dy in d:
        if 0 <= x + dx < H and 0 <= y + dy < W and (not judged[x + dx][y + dy]) and orchard[x + dx][y + dy] == c:
            dfs(x + dx, y + dy, c)


if __name__ == '__main__':
    while True:
        H, W = list(map(int, input().split(' ')))
        if H == 0 and W == 0:
            break
        orchard = [input() for i in range(H)]
        judged = [[False for i in range(W)] for j in range(H)]
        # print(orchard)
        ans = 0
        for i in range(0, H):
            for j in range(0, W):
                if not judged[i][j]:
                    ans += 1
                    dfs(i, j, orchard[i][j])

        print(ans)

Submission status: Runtime Error

Solution code:
import sys

d = [(1, 0), (-1, 0), (0, 1), (0, -1)]


def dfs(x, y, c):
    judged[x][y] = True
    for dx, dy in d:
        if 0 <= x + dx < H and 0 <= y + dy < W and (not judged[x + dx][y + dy]) and orchard[x + dx][y + dy] == c:
            dfs(x + dx, y + dy, c)


if __name__ == '__main__':
    while True:
        H, W = list(map(int, input().split()))
        if H == 0 and W == 0:
            break
        orchard = [input() for i in range(H)]
        judged = [[False for i in range(W)] for j in range(H)]
        # print(orchard)
        ans = 0
        for i in range(0, H):
            for j in range(0, W):
                if not judged[i][j]:
                    ans += 1
                    dfs(i, j, orchard[i][j])

        print(ans)
        input()

Submission status: Runtime Error

Solution code:
def infection(i, j):
    if farm[i][j] == moji:
        farm[i][j] = '+';
        if j + 1 < W : infection(i, j + 1)
        if i + 1 < H : infection(i + 1, j)
        if j - 1 >= 0 :infection(i, j - 1)
        if i - 1 >= 0 :infection(i - 1, j)

while(1):
    farm = []
    cnt = 0;
    H, W = list(map(int, input().split()))
    if H == 0 and W == 0: break

    for i in range(H):
        farm.append(list(input()))

    for i in range(H):
        for j in range(W):
            if farm[i][j] == '+': continue
            cnt+=1
            moji = farm[i][j]
            infection(i, j)
    print(cnt)

Submission status: Runtime Error

Solution code:
def infection(i, j):
    if farm[i][j] == moji:
        farm[i][j] = '+'
        if j + 1 < W : infection(i, j + 1)
        if i + 1 < H : infection(i + 1, j)
        if j - 1 >= 0 :infection(i, j - 1)
        if i - 1 >= 0 :infection(i - 1, j)

while(1):
    farm = []
    cnt = 0
    H, W = list(map(int, input().split()))
    if H == 0 and W == 0: break

    for i in range(H):
        farm.append(list(input()))

    for i in range(H):
        for j in range(W):
            if farm[i][j] == '+': continue
            cnt += 1
            moji = farm[i][j]
            infection(i, j)
    print(cnt)

Submission status: Runtime Error

Solution code:
def infection(i, j,H, W, moji):
    if farm[i][j] == moji:
        farm[i][j] = '+'
        if j + 1 < W : infection(i, j + 1,H,W,moji)
        if i + 1 < H : infection(i + 1, j,H,W,moji)
        if j - 1 >= 0 :infection(i, j - 1,H,W,moji)
        if i - 1 >= 0 :infection(i - 1, j,H,W,moji)

while(1):
    farm = []
    cnt = 0
    H, W = list(map(int, input().split()))
    if H == 0 and W == 0: break

    for i in range(H):
        farm.append(list(input()))

    for i in range(H):
        for j in range(W):
            if farm[i][j] == '+': continue
            cnt += 1
            infection(i, j,H,W,moji)
    print(cnt)

Submission status: Runtime Error

Solution code:
def infection(i, j,H, W, moji):
    if farm[i][j] == moji:
        farm[i][j] = '+'
        if j + 1 < W : infection(i, j + 1,H,W,moji)
        if i + 1 < H : infection(i + 1, j,H,W,moji)
        if j - 1 >= 0 :infection(i, j - 1,H,W,moji)
        if i - 1 >= 0 :infection(i - 1, j,H,W,moji)

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

    for i in range(H):
        farm.append(list(input()))

    for i in range(H):
        for j in range(W):
            if farm[i][j] == '+': continue
            cnt += 1
            infection(i, j,H,W,moji)
    print(cnt)

Submission status: Runtime Error

Solution code:
def infection(i, j):
    if farm[i][j] == moji:
        farm[i][j] = '+'
        if j + 1 < W : infection(i, j + 1)
        if i + 1 < H : infection(i + 1, j)
        if j - 1 >= 0 :infection(i, j - 1)
        if i - 1 >= 0 :infection(i - 1, j)

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

    for i in range(H):
        farm.append(list(input()))

    for i in range(H):
        for j in range(W):
            if farm[i][j] == '+': continue
            cnt += 1
            moji = farm[i][j]
            infection(i, j)
    print(cnt)

Submission status: Runtime Error

Solution code:
def search(table, visited, i, j, c):
    visited[i][j] = True

    if table[i-1][j] == c and visited[i-1][j] == False:
        search(table, visited, i-1, j, c)
    if table[i+1][j] == c and visited[i+1][j] == False:
        search(table, visited, i+1, j, c)
    if table[i][j-1] == c and visited[i][j-1] == False:
        search(table, visited, i, j-1, c)
    if table[i][j+1] == c and visited[i][j+1] == False:
        search(table, visited, i, j+1, c)

    return

while True:
    H,W = [int(i) for i in input().split()]
    if H == 0 and W == 0:
        break

    visited = [[False for i in range(W+2)] for j in range(H+2)]
    table = [["." for i in range(W+2)] for j in range(H+2)]

    for i in range(1, H+1):
        s = input()
        for j in range(1, W+1):
            table[i][j] = s[j-1]

    ans = 0
    for i in range(1, H+1):
        for j in range(1, W+1):
            if visited[i][j] == False:
                ans = ans + 1
                search(table, visited, i, j, table[i][j])
    print(ans)

Submission status: Runtime Error

Solution code:
while True:
  h, w = map(int, input().split())
  if not h:
    break

  mp = [list("X" + input() + "X") for _ in range(h)]
  mp.insert(0, ["X"] * (w + 2))
  mp.append(["X"] * (w + 2))
  
  direct = ((0, 1), (0, -1), (1, 0), (-1, 0))

  def search(x, y, target):
    mp[y][x] = "X"
    for dx, dy in direct:
      nx, ny = x + dx, y + dy
      if mp[ny][nx] == target:
        search(nx, ny, target)
  
  ans = 0

  for x in range(1, w + 1):
    for y in range(1, h + 1):
      target = mp[y][x]
      if target != "X":
        search(x, y, target)
        ans += 1
  
  print(ans)

Submission status: Runtime Error

Solution code:
mv=((0,1),(0,-1),(1,0),(-1,0))
def dfs(y,x):
    c=L[y][x]
    L[y][x]="."
    for dx,dy in mv:
        mx=x+dx
        my=y+dy
        if 0<=mx<W and 0<=my<H and L[my][mx]==c:
            dfs(my,mx)
while True:
    H,W=map(int,raw_input().split())
    if H==W==0:break
    L=[list(raw_input()) for i in range(H)]
    cnt=0
    for i in range(H):
        for j in range(W):
            if L[i][j]!=".":
                dfs(i,j)
                cnt+=1
    print cnt

Submission status: Runtime Error

Solution code:
def solve(s,i,x,y):
	A[y][x] = i
	if x < W-1 and A[y][x+1] == s: solve(s,i,x+1,y)
	if y < H-1 and A[y+1][x] == s: solve(s,i,x,y+1)
	if x > 0   and A[y][x-1] == s: solve(s,i,x-1,y)
	if y > 0   and A[y-1][x] == s: solve(s,i,x,y-1)
	
while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	i = 1
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				solve(A[y][x],i,x,y)
				i += 1
	print max([max(A[i]) for i in range(H)])

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	A[y][x] = 0
	if x < W-1 and A[y][x+1] == s: fill0(s,x+1,y)
	if y < H-1 and A[y+1][x] == s: fill0(s,x,y+1)
	if x > 0   and A[y][x-1] == s: fill0(s,x-1,y)
	if y > 0   and A[y-1][x] == s: fill0(s,x,y-1)

while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	i = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(A[y][x],x,y)
				i += 1
	print i

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	A[y][x] = 0
	if x < W-1 and A[y][x+1] == s: fill0(s,x+1,y)
	if y < H-1 and A[y+1][x] == s: fill0(s,x,y+1)
	if x > 0   and A[y][x-1] == s: fill0(s,x-1,y)
	if y > 0   and A[y-1][x] == s: fill0(s,x,y-1)

while 1:
	H,W = map(int,raw_input().split())
	if H == 0 and W == 0: break
	A = [list(raw_input()) for i in range(H)]
	i = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(A[y][x],x,y)
				i += 1
	print i

Submission status: Runtime Error

Solution code:
def fill0(A,s,x,y):
	A[y][x] = 0
	if x < W-1 and A[y][x+1] == s: fill0(A,s,x+1,y)
	if y < H-1 and A[y+1][x] == s: fill0(A,s,x,y+1)
	if x > 0   and A[y][x-1] == s: fill0(A,s,x-1,y)
	if y > 0   and A[y-1][x] == s: fill0(A,s,x,y-1)

while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(A,A[y][x],x,y)
				ans += 1
	print ans

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	if 0 <= x <= W-1 and 0 <= y <= H-1 and A[y][x] == s:
		A[y][x] = 0
		fill0(s,x+1,y); fill0(s,x,y+1); fill0(s,x-1,y); fill0(s,x,y-1)

while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(A[y][x],x,y)
				ans += 1
	print ans

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	if 0 <= x <= W-1 and 0 <= y <= H-1 and A[y][x] == s:
		A[y][x] = 0
		fill0(s,x+1,y); fill0(s,x,y+1); fill0(s,x-1,y); fill0(s,x,y-1)
	return
while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(A[y][x],x,y)
				ans += 1
	print ans

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	if 0 <= x <= W-1 and 0 <= y <= H-1 and A[y][x] == s:
		A[y][x] = 0
		fill0(s,x+1,y); fill0(s,x,y+1); fill0(s,x-1,y); fill0(s,x,y-1)
while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(A[y][x],x,y)
				ans += 1
	print(ans)

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	if 0 <= x <= W-1 and 0 <= y <= H-1 and A[y][x] == s:
		A[y][x] = 0
		fill0(s,x+1,y); fill0(s,x,y+1); fill0(s,x-1,y); fill0(s,x,y-1)
while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	print 1

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	if 0 <= x <= W-1 and 0 <= y <= H-1 and A[y][x] == s:
		A[y][x] = 0
		fill0(s,x+1,y); fill0(s,x,y+1); fill0(s,x-1,y); fill0(s,x,y-1)
while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	if 0 <= x <= W-1 and 0 <= y <= H-1 and A[y][x] == s:
		A[y][x] = 0
		fill0(s,x+1,y); fill0(s,x,y+1); fill0(s,x-1,y); fill0(s,x,y-1)
while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(A[y][x],x,y)
				ans += 1
	print 0

Submission status: Runtime Error

Solution code:
def fill0(A,H,W,s,x,y):
	if 0 <= x <= W-1 and 0 <= y <= H-1 and A[y][x] == s:
		A[y][x] = 0
		fill0(A,H,W,s,x+1,y); fill0(A,H,W,s,x,y+1); fill0(A,H,W,s,x-1,y); fill0(A,H,W,s,x,y-1)
while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(H,W,A[y][x],x,y)
				ans += 1
	print ans

Submission status: Runtime Error

Solution code:
def fill(s,x,y):
	if 0 <= x <= W-1 and 0 <= y <= H-1 and A[y][x] == s:
		A[y][x] = 0
		fill(s,x+1,y); fill(s,x,y+1); fill(s,x-1,y); fill(s,x,y-1)
while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] != 0:
				fill(A[y][x],x,y)
				ans += 1
	print ans

Submission status: Runtime Error

Solution code:
def fill(s,x,y):
	if 0 <= x <= W-1 and 0 <= y <= H-1 and A[y][x] == s:
		A[y][x] = 0
		fill(s,x+1,y); fill(s,x,y+1); fill(s,x-1,y); fill(s,x,y-1)
while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for b in range(H):
		for a in range(W):
			if A[b][a] != 0:
				fill(A[b][a],a,b)
				ans += 1
	print ans

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
     A[y][x] = 0
     if x < W-1 and A[y][x+1] == s: fill0(s,x+1,y)
     if y < H-1 and A[y+1][x] == s: fill0(s,x,y+1)
     if x > 0   and A[y][x-1] == s: fill0(s,x-1,y)
     if y > 0   and A[y-1][x] == s: fill0(s,x,y-1)

while 1:
     H,W = map(int,raw_input().split())
     if H == 0: break
     A = [list(raw_input()) for i in range(H)]
     ans = 0
     for y in range(H):
          for x in range(W):
               if A[y][x] in ["#","@","*"]:
					try:
	                    fill0(A[y][x],x,y)
	                    ans += 1
                    except:
                    	pass
     print ans

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	try: A[y][x] = 0
	except: pass
	if x < W-1 and A[y][x+1] == s: fill0(s,x+1,y)
	if y < H-1 and A[y+1][x] == s: fill0(s,x,y+1)
	if x > 0   and A[y][x-1] == s: fill0(s,x-1,y)
	if y > 0   and A[y-1][x] == s: fill0(s,x,y-1)

while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(A[y][x],x,y)
				ans += 1
	print ans

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	A[y][x] = 0
	if x < W-1 and A[y][x+1] == s: fill0(s,x+1,y)
	if y < H-1 and A[y+1][x] == s: fill0(s,x,y+1)
	if x > 0   and A[y][x-1] == s: fill0(s,x-1,y)
	if y > 0   and A[y-1][x] == s: fill0(s,x,y-1)

while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for yi in range(H):
		for xi in range(W):
			if A[yi][xi] in ["#","@","*"]:
				fill0(A[yi][xi],xi,yi)
				ans += 1
	print ans

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	A[y][x] = 0
	try: if x < W-1 and A[y][x+1] == s: fill0(s,x+1,y)
	except: pass
	try: if y < H-1 and A[y+1][x] == s: fill0(s,x,y+1)
	except: pass
	try: if x > 0   and A[y][x-1] == s: fill0(s,x-1,y)
	except: pass
	try: if y > 0   and A[y-1][x] == s: fill0(s,x,y-1)
	except: pass

while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(A[y][x],x,y)
				ans += 1
	print ans

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	try: A[y][x] = 0
	except: pass
	try: if x < W-1 and A[y][x+1] == s: fill0(s,x+1,y)
	except: pass
	try: if y < H-1 and A[y+1][x] == s: fill0(s,x,y+1)
	except: pass
	try: if x > 0   and A[y][x-1] == s: fill0(s,x-1,y)
	except: pass
	try: if y > 0   and A[y-1][x] == s: fill0(s,x,y-1)
	except: pass

while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(A[y][x],x,y)
				ans += 1
	print ans

Submission status: Runtime Error

Solution code:
def fill0(s,x,y):
	global A
	A[y][x] = 0
	if x < W-1 and A[y][x+1] == s: fill0(s,x+1,y)
	if y < H-1 and A[y+1][x] == s: fill0(s,x,y+1)
	if x > 0   and A[y][x-1] == s: fill0(s,x-1,y)
	if y > 0   and A[y-1][x] == s: fill0(s,x,y-1)

while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [list(raw_input()) for i in range(H)]
	ans = 0
	for y in range(H):
		for x in range(W):
			if A[y][x] in ["#","@","*"]:
				fill0(A[y][x],x,y)
				ans += 1
	print ans