#include <stdio.h>
#include <stdlib.h>
int visit[100][100];
char array[100][100];
int x[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int y[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int m, n;
static void dfs(char array[][100], int i, int j);
int main(int argc, char **argv)
{
int i, j, num = 0;
scanf("%d %d", &m, &n);
getchar();
for(i = 0; i < m; i++){
for(j = 0; j < n; j++){
scanf("%c", &array[i][j]);
}
getchar();
}
for(i = 0; i < m; i++){
for(j = 0; j < n; j++){
if((array[i][j] == 'W') && (visit[i][j] == 0)){
dfs(array, i, j);
num++;
}
}
}
printf("%d\n", num);
exit(0);
}
static void dfs(char array[][100], int i, int j)
{
int p, flag, newrow, newcol;
visit[i][j] = 1;
for(p = 0; p < 8; p++){
newrow = i + x[p];
newcol = j + y[p];
flag = ((array[newrow][newcol] == 'W') && (visit[newrow][newcol] == 0)
&& (newrow >= 0) && (newrow < m) && (newcol >= 0) && (newcol < n));
if(flag){
dfs(array, newrow, newcol);
}
}
}
|