Buggy python submission (verdict: Runtime Error):
while True:
    a=[int(num)for num in input().split(' ')]
    if a[0]==0:break
    s=[]
    cnt=0
    def rep(r,c,nest):
        global cnt
        f=s[r][c]
        if f=='.':return
        s[r][c]='.'
        if r>0:
            if s[r-1][c]==f:rep(r-1,c,False)
        if r<a[0]-1:
            if s[r+1][c]==f:rep(r+1,c,False)
        if c>0:
            if s[r][c-1]==f:rep(r,c-1,False)
        if c<a[1]-1:
            if s[r][c+1]==f:rep(r,c+1,False)
        if nest==True:
            cnt+=1 
    for i in range(a[0]):
        s.append(list(input()))
    for i in range(a[0]):
        for j in range(a[1]):
            rep(i,j,True)
    print(cnt)
    

Fixed submission:
''' DFS(TLE)
while True:
    a=[int(num)for num in input().split(' ')]
    if a[0]==0:break
    s=[]
    cnt=0
    def rep(r,c,nest):
        global cnt
        f=s[r][c]
        if f=='.':return
        s[r][c]='.'
        if r>0:
            if s[r-1][c]==f:rep(r-1,c,False)
        if r<a[0]-1:
            if s[r+1][c]==f:rep(r+1,c,False)
        if c>0:
            if s[r][c-1]==f:rep(r,c-1,False)
        if c<a[1]-1:
            if s[r][c+1]==f:rep(r,c+1,False)
        if nest==True:
            cnt+=1 
    for i in range(a[0]):
        s.append(list(input()))
    for i in range(a[0]):
        for j in range(a[1]):
            rep(i,j,True)
    print(cnt)
'''
#
''' BFS(List)
import queue
dr=[0,0,1,-1]
dc=[1,-1,0,0]
while True:
    a=[int(num)for num in input().split(' ')]
    if a[0]==0:break
    s=[]
    cnt=0
    for i in range(a[0]):
        s.append(list(input()))
    q=queue.Queue()
    for i in range(a[0]):
        for j in range(a[1]):
            if s[i][j]=='.':continue
            f=s[i][j]
            l=[[i,j]]
            while len(l)!=0:
                next_depth=[]
                for cor in l:
                    for k in range(4):
                        r=cor[0]+dr[k]
                        c=cor[1]+dc[k]
                        if 0<=r<a[0] and 0<=c<a[1] and s[r][c]==f:
                            s[r][c]='.'
                            next_depth.append([r,c])
                l=next_depth
            cnt+=1
    print(cnt)
'''
#
''' BFS(Queue) '''
import queue
dr=[0,0,1,-1]
dc=[1,-1,0,0]
while True:
    a=[int(num)for num in input().split(' ')]
    if a[0]==0:break
    s=[]
    cnt=0
    for i in range(a[0]):
        s.append(list(input()))
    q=queue.Queue()
    for i in range(a[0]):
        for j in range(a[1]):
            if s[i][j]=='.':continue
            f=s[i][j]
            q.put([i,j])
            while not q.empty():
                cor=q.get()
                for k in range(4):
                    r=cor[0]+dr[k]
                    c=cor[1]+dc[k]
                    if 0<=r<a[0] and 0<=c<a[1] and s[r][c]==f:
                        s[r][c]='.'
                        q.put([r,c])
            cnt+=1
    print(cnt)

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(9000)
while True:
    a=[int(num)for num in input().split(' ')]
    if a[0]==0:break
    s=[]
    cnt=0
    def rep(r,c,nest):
        global cnt
        f=s[r][c]
        if f=='.':return
        s[r][c]='.'
        if r>0:
            if s[r-1][c]==f:rep(r-1,c,False)
        if r<a[0]-1:
            if s[r+1][c]==f:rep(r+1,c,False)
        if c>0:
            if s[r][c-1]==f:rep(r,c-1,False)
        if c<a[1]-1:
            if s[r][c+1]==f:rep(r,c+1,False)
        if nest==True:
            cnt+=1 
    for i in range(a[0]):
        s.append(list(input()))
    for i in range(a[0]):
        for j in range(a[1]):
            rep(i,j,True)
    print(cnt)

Fixed submission:
''' DFS(TLE)
while True:
    a=[int(num)for num in input().split(' ')]
    if a[0]==0:break
    s=[]
    cnt=0
    def rep(r,c,nest):
        global cnt
        f=s[r][c]
        if f=='.':return
        s[r][c]='.'
        if r>0:
            if s[r-1][c]==f:rep(r-1,c,False)
        if r<a[0]-1:
            if s[r+1][c]==f:rep(r+1,c,False)
        if c>0:
            if s[r][c-1]==f:rep(r,c-1,False)
        if c<a[1]-1:
            if s[r][c+1]==f:rep(r,c+1,False)
        if nest==True:
            cnt+=1 
    for i in range(a[0]):
        s.append(list(input()))
    for i in range(a[0]):
        for j in range(a[1]):
            rep(i,j,True)
    print(cnt)
'''
#
''' BFS(List)
import queue
dr=[0,0,1,-1]
dc=[1,-1,0,0]
while True:
    a=[int(num)for num in input().split(' ')]
    if a[0]==0:break
    s=[]
    cnt=0
    for i in range(a[0]):
        s.append(list(input()))
    q=queue.Queue()
    for i in range(a[0]):
        for j in range(a[1]):
            if s[i][j]=='.':continue
            f=s[i][j]
            l=[[i,j]]
            while len(l)!=0:
                next_depth=[]
                for cor in l:
                    for k in range(4):
                        r=cor[0]+dr[k]
                        c=cor[1]+dc[k]
                        if 0<=r<a[0] and 0<=c<a[1] and s[r][c]==f:
                            s[r][c]='.'
                            next_depth.append([r,c])
                l=next_depth
            cnt+=1
    print(cnt)
'''
#
''' BFS(Queue) '''
import queue
dr=[0,0,1,-1]
dc=[1,-1,0,0]
while True:
    a=[int(num)for num in input().split(' ')]
    if a[0]==0:break
    s=[]
    cnt=0
    for i in range(a[0]):
        s.append(list(input()))
    q=queue.Queue()
    for i in range(a[0]):
        for j in range(a[1]):
            if s[i][j]=='.':continue
            f=s[i][j]
            q.put([i,j])
            while not q.empty():
                cor=q.get()
                for k in range(4):
                    r=cor[0]+dr[k]
                    c=cor[1]+dc[k]
                    if 0<=r<a[0] and 0<=c<a[1] and s[r][c]==f:
                        s[r][c]='.'
                        q.put([r,c])
            cnt+=1
    print(cnt)