#include<stdio.h> int p[2505]; char s[55][55]; int find(int i) { if(p[i]!=i) p[i]=find(p[i]); return p[i]; } int main(void) { int m,n; while(scanf("%d%d",&m,&n)!=EOF&&(m!=-1||n!=-1)) { int i,a,b,j,x=m*n; for(i=1;i<=2500;i++) p[i]=i; for(i=1;i<=m;i++) {getchar(); for(j=1;j<=n;j++) scanf("%c",&s[i][j]);} for(i=1;i<=m;i++) for(j=1;j<n;j++) if((s[i][j]=='B'||s[i][j]=='D'||s[i][j]=='F'||s[i][j]=='G'||s[i][j]=='I'||s[i][j]=='J'||s[i][j]=='K')&&(s[i][j+1]=='A'||s[i][j+1]=='C'||s[i][j+1]=='F'||s[i][j+1]=='G'||s[i][j+1]=='H'||s[i][j+1]=='I'||s[i][j+1]=='K')) {a=find((i-1)*n+j);b=find((i-1)*n+j+1); if(a!=b) {p[b]=a;x--;}} for(j=1;j<=n;j++) for(i=1;i<m;i++) if((s[i][j]=='C'||s[i][j]=='D'||s[i][j]=='E'||s[i][j]=='H'||s[i][j]=='I'||s[i][j]=='J'||s[i][j]=='K')&&(s[i+1][j]=='A'||s[i+1][j]=='B'||s[i+1][j]=='E'||s[i+1][j]=='G'||s[i+1][j]=='H'||s[i+1][j]=='J'||s[i+1][j]=='K')) {a=find((i-1)*n+j);b=find(i*n+j); if(a!=b) {p[b]=a;x--;} } printf("%d\n",x); } return 0; }