//深搜一开始一直SF,后来又把上下左右方向给搞乱了
//总之做题还是不够细心,哎。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 51
using namespace std;
char map[MAX][MAX];
bool vis[MAX][MAX];
int m,n;
int num;
struct Node{
bool up,down,left,right;
};
Node land[11] = {{1,0,1,0},{1,0,0,1},{0,1,1,0},{0,1,0,1},{1,1,0,0},
{0,0,1,1},{1,0,1,1,},{1,1,1,0},{0,1,1,1},{1,1,0,1},{1,1,1,1}};
int dir[4][2]=
{
-1,0,//上右下左
0,1,
1,0,
0,-1,
};
void dfs(int x,int y)
{
int i;
vis[x][y]=1;
int xx,yy;
for(i=0; i<4; i++)
{
xx=x+dir[i][0],yy=y+dir[i][1];
if(xx<0||yy<0||xx>=m||yy>=n||vis[xx][yy]) continue;
int point1=map[xx][yy]-'A';
int point2=map[x][y]-'A';
switch(i){
case 0:
if(land[point1].down&&land[point2].up)
dfs(xx,yy);
break;
case 1:
if(land[point1].left&&land[point2].right)
dfs(xx,yy);
break;
case 2:
if(land[point1].up&&land[point2].down)
dfs(xx,yy);
break;
case 3:
if(land[point1].right&&land[point2].left)
dfs(xx,yy);
break;
}
}
}
int main()
{
int i,j;
while(cin>>m>>n)
{
if(m<0||n<0) break;
num=0;
memset(vis,0,sizeof(vis));
getchar();
for(i=0; i<m; i++)
scanf("%s",map[i]);
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
if(!vis[i][j])
{
num++;
dfs(i,j);
}
}
}
cout<<num<<endl;
}
return 0;
}