from collections import defaultdict class Unionfind: def __init__(self, N): self.par = [-1] * N self.rank = [1] * N def root(self, x): if self.par[x] < 0: return x self.par[x] = self.root(self.par[x]) return self.par[x] def unite(self, x, y): rx, ry = self.root(x), self.root(y) if rx != ry: if self.rank[rx] >= self.rank[ry]: self.par[rx] += self.par[ry] self.par[ry] = rx if self.rank[rx] == self.rank[ry]: self.rank[rx] += 1 else: self.par[ry] += self.par[rx] self.par[rx] = ry def is_same(self, x, y): return self.root(x) == self.root(y) def count(self, x): return -self.par[self.root(x)] N, M = map(int, input().split()) S = [input() for _ in range(N)] uf = Unionfind(N*M) for i in range(N): for j in range(M): for ni, nj in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]: if 0<=ni