#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 50;
int father[maxn * maxn + 100];
char mat[maxn + 5][maxn + 5];
int n, m;
int get_father(int a) {
//return father[a] == -1 ? (father[a] = a) : (father[a] = get_father(a));
return father[a] == a ? a : (father[a] = get_father(father[a]));
}
void get_union(int a, int b) {
int c = get_father(a);
int d = get_father(b);
father[c] = d;
}
bool ok_up(char a, char b) {//a在下,b在上
if(a == 'A' || a == 'B' || a == 'E' || a == 'G' || a == 'H' || a == 'J' || a == 'K') {
if(b == 'C' || b == 'D' || b == 'E' || b == 'H' || b == 'I' || b == 'J' || b == 'K') return true;
else return false;
}
else return false;
}
bool ok_left(char a, char b) {//a在右,b在左
if(a == 'A' || a == 'C' || a == 'F' || a == 'G' || a == 'H' || a == 'I' || a == 'K') {
if(b == 'B' || b == 'D' || b == 'F' || b == 'G' || b == 'I' || b == 'J' || b == 'K') return true;
else return false;
}
else return false;
}
void con_up(int a, int b, int c, int d) {
if(ok_up(mat[a][b], mat[c][d])) get_union( a * m + b, c *m + d);
}
void con_left(int a, int b, int c, int d) {
if(ok_left(mat[a][b], mat[c][d])) get_union( a * m + b, c * m + d);
}
void judge(int a, int b) {
if(a > 0) con_up(a, b, a - 1, b);
if(b > 0) con_left(a, b, a, b - 1);
}
int main() {
while(~scanf("%d%d", &n, &m)) {
if(n == -1 && m == -1) return 0;
//memset(father, -1, sizeof(father));
for(int i = 0; i < n * m + 10; i++) father[i] = i;
int cnt = 0;
for(int i = 0; i < n; i++) {
getchar();
for(int j = 0; j < m; j++) {
scanf("%c", &mat[i][j]);
judge(i, j);
}
}
//cout << "here" << endl;
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { if(father[i * m + j] == i * m + j || father[i * m + j] == -1) cnt++;}
printf("%d\n", cnt);
}
return 0;
}
AC了一道并查集,感觉挺简单的~~~