import sequtils,queues proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "" .} proc scan(): int32 = while true: let k = getchar_unlocked() if k < '0': break result = 10.int32 * result + k.ord.int32 - '0'.ord.int32 type UnionFind[T] = object parent : seq[T] rank : seq[int16] proc root[T](self:var UnionFind[T],x:T): T = if self.parent[x] == x: return x self.parent[x] = self.root(self.parent[x]) return self.parent[x] proc initUnionFind[T](size:int) : UnionFind[T] = result.parent = newSeq[T](size) result.rank = newSeq[int16](size) for i in 0.int32.. 0 and uf.parent[i-1] != 0: uf.unite(i,i-1) if y > 0 and uf.parent[i-w] != 0: uf.unite(i,i-w) else: uf.parent[i] = 0 discard getchar_unlocked() i += 1 var ans = 0 for i in 1..h*w: if uf.root(i) == i : ans += 1 echo ans