Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import sys
import os
import math

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

for s in sys.stdin:
    H, W = map(int, s.split())
    if H == W == 0:
        break

    # map
    M = []
    for i in range(H):
        s = input().strip()
        M.append(s)

    # W x H white map
    A = [[None for i in range(H)] for j in range(W)]

    def drawable(x, y):
        if 0 <= x < W and 0 <= y < H and A[y][x] == None:
            return True
        else:
            return False

    color_num = 0

    def fill(x, y):
        global color_num

        A[y][x] = color_num
        for direction in directions:
            new_x = x + direction[0]
            new_y = y + direction[1]
            if drawable(new_x, new_y) and M[y][x] == M[new_y][new_x]:
                fill(new_x, new_y)

    for y in range(H):
        for x in range(W):
            if drawable(x, y):
                color_num += 1
                fill(x, y)

    print(color_num)

Fixed submission:
# -*- coding: utf-8 -*-

import sys
import os
import math

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

for s in sys.stdin:
    H, W = map(int, s.split())
    if H == W == 0:
        break

    # map
    M = []
    for i in range(H):
        s = input().strip()
        M.append(s)

    # W x H white map
    A = [[None for i in range(W)] for j in range(H)]

    def drawable(x, y):
        if 0 <= x < W and 0 <= y < H and A[y][x] == None:
            return True
        else:
            return False

    color_num = 0

    def fill(x, y):
        global color_num

        Q = [(x, y)]
        while Q:
            p = Q.pop(0)
            _x = p[0]
            _y = p[1]
            A[_y][_x] = color_num
            for direction in directions:
                new_x = _x + direction[0]
                new_y = _y + direction[1]
                if drawable(new_x, new_y) and M[_y][_x] == M[new_y][new_x]:
                    A[new_y][new_x] = color_num
                    Q.append((new_x, new_y))

    for y in range(H):
        for x in range(W):
            if drawable(x, y):
                color_num += 1
                fill(x, y)

    print(color_num)

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import sys
import os
import math

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

for s in sys.stdin:
    H, W = map(int, s.split())
    if H == W == 0:
        break

    # map
    M = []
    for i in range(H):
        s = input().strip()
        M.append(s)

    # W x H white map
    A = [[None for i in range(W)] for j in range(H)]

    def drawable(x, y):
        if 0 <= x < W and 0 <= y < H and A[y][x] == None:
            return True
        else:
            return False

    color_num = 0

    def fill(x, y):
        global color_num

        A[y][x] = color_num
        for direction in directions:
            new_x = x + direction[0]
            new_y = y + direction[1]
            if drawable(new_x, new_y) and M[y][x] == M[new_y][new_x]:
                fill(new_x, new_y)

    for y in range(H):
        for x in range(W):
            if drawable(x, y):
                color_num += 1
                fill(x, y)

    print(color_num)

Fixed submission:
# -*- coding: utf-8 -*-

import sys
import os
import math

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

for s in sys.stdin:
    H, W = map(int, s.split())
    if H == W == 0:
        break

    # map
    M = []
    for i in range(H):
        s = input().strip()
        M.append(s)

    # W x H white map
    A = [[None for i in range(W)] for j in range(H)]

    def drawable(x, y):
        if 0 <= x < W and 0 <= y < H and A[y][x] == None:
            return True
        else:
            return False

    color_num = 0

    def fill(x, y):
        global color_num

        Q = [(x, y)]
        while Q:
            p = Q.pop(0)
            _x = p[0]
            _y = p[1]
            A[_y][_x] = color_num
            for direction in directions:
                new_x = _x + direction[0]
                new_y = _y + direction[1]
                if drawable(new_x, new_y) and M[_y][_x] == M[new_y][new_x]:
                    A[new_y][new_x] = color_num
                    Q.append((new_x, new_y))

    for y in range(H):
        for x in range(W):
            if drawable(x, y):
                color_num += 1
                fill(x, y)

    print(color_num)

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import sys
import os
import math

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

for s in sys.stdin:
    H, W = map(int, s.split())
    if H == W == 0:
        break

    # map
    M = []
    for i in range(H):
        s = input().strip()
        M.append(s)

    # W x H white map
    A = [[None for i in range(W)] for j in range(H)]

    def drawable(x, y):
        if 0 <= x < W and 0 <= y < H and A[y][x] == None:
            return True
        else:
            return False

    color_num = 0

    def fill(x, y):
        global color_num

        A[y][x] = color_num
        for direction in directions:
            new_x = x + direction[0]
            new_y = y + direction[1]
            if drawable(new_x, new_y) and M[y][x] == M[new_y][new_x]:
                fill(new_x, new_y)

    for y in range(H):
        for x in range(W):
            if drawable(x, y):
                color_num += 1
                fill(x, y)

    print(color_num)

Fixed submission:
# -*- coding: utf-8 -*-

import sys
import os
import math

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

for s in sys.stdin:
    H, W = map(int, s.split())
    if H == W == 0:
        break

    # map
    M = []
    for i in range(H):
        s = input().strip()
        M.append(s)

    # W x H white map
    A = [[None for i in range(W)] for j in range(H)]

    def drawable(x, y):
        if 0 <= x < W and 0 <= y < H and A[y][x] == None:
            return True
        else:
            return False

    color_num = 0

    def fill(x, y):
        global color_num

        Q = [(x, y)]
        while Q:
            p = Q.pop(0)
            _x = p[0]
            _y = p[1]
            A[_y][_x] = color_num
            for direction in directions:
                new_x = _x + direction[0]
                new_y = _y + direction[1]
                if drawable(new_x, new_y) and M[_y][_x] == M[new_y][new_x]:
                    A[new_y][new_x] = color_num
                    Q.append((new_x, new_y))

    for y in range(H):
        for x in range(W):
            if drawable(x, y):
                color_num += 1
                fill(x, y)

    print(color_num)