#include <bits/stdc++.h>
using namespace std;
bool used[100001];
long long int n, m, lnt = 0, cnt = 1, a[1001][1001];
vector<int> g[100001];
char x;
void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (!used[to]) dfs(to);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> x;
      if (x == 'B') {
        a[i][j] = cnt;
        cnt++;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] != 0) {
        if (a[i + 1][j] != 0) g[a[i][j]].push_back(a[i + 1][j]);
        if (a[i][j + 1] != 0) g[a[i][j]].push_back(a[i][j + 1]);
        if (a[i][j - 1] != 0) g[a[i][j]].push_back(a[i][j - 1]);
        if (a[i - 1][j] != 0) g[a[i][j]].push_back(a[i - 1][j]);
        if (a[i + 1][j] != 0) g[a[i + 1][j]].push_back(a[i][j]);
        if (a[i][j + 1] != 0) g[a[i][j + 1]].push_back(a[i][j]);
        if (a[i][j - 1] != 0) g[a[i][j - 1]].push_back(a[i][j]);
        if (a[i - 1][j] != 0) g[a[i - 1][j]].push_back(a[i][j]);
      }
    }
  }
  for (int i = 1; i < cnt; i++) {
    if (!used[i]) {
      lnt++;
      dfs(i);
    }
  }
  printf("%d", lnt);
}
