import std.stdio : readln, writeln;
import std.array : split;
import std.conv : to;
import std.typecons;
import std.algorithm : map;
import std.range : iota;
import std.string : chomp;

char[Tuple!(int, int)] field;
int W, H;

void dfs(int x, int y, char f) {
  field[tuple(x, y)] = 'a';
  foreach (dx; [-1, 1]) {
    int nx = x + dx;
    if (0 <= nx && nx < W
        && field[tuple(nx, y)] == f) dfs(nx, y, f);
  }
  foreach (dy; [-1, 1]) {
    int ny = y + dy;
    if (0 <= ny && ny < H
        && field[tuple(x, ny)] == f) dfs(x, ny, f);
  }
}

int main() {
  auto input = map!(to!int)(split(chomp(readln()))[0 .. 2]);
  W = input[0], H = input[1];
  
  foreach (int y; iota(H))
    foreach (int x, char v; readln()[0 .. W])
      field[tuple(x, y)] = v;

  int res = 0;
  while (1) {
    foreach (int y; iota(H)) {
      foreach (int x; iota(W)) {
        char f = field[tuple(x, y)];
        if (f != 'a') {
          dfs(x, y, f);
          res++;
        }
      }
    }
    auto end = map!(to!int)(split(chomp(readln()))[0 .. 2]);
    if (end[0] == 0 && end[1] == 0) break;
  }
  writeln(res);
  return 0;
}