import sequtils proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "" .} proc scan(): int = while true: let k = getchar_unlocked() if k < '0': break result = 10 * result + k.ord - '0'.ord template useStack() = type Stack*[T] = ref object data: seq[T] size: int index: int proc newStack*[T](size: int = 64): Stack[T] = new(result) result.data = newSeq[T](size) result.size = size result.index = -1 proc isEmpty*[T](self: Stack[T]): bool = self.index < 0 proc isValid*[T](self: Stack[T]): bool = self.index >= 0 and self.index < self.size proc len*[T](self: Stack[T]): int = if self.isEmpty(): return 0 return self.index + 1 proc top*[T](self: Stack[T]): T = assert self.isValid() return self.data[self.index] proc pop*[T](self: var Stack[T]): T {.discardable.} = assert self.index >= 0 result = self.top() self.index -= 1 proc push*[T](self: var Stack[T], elem: T) = self.index += 1 if self.index < self.size: self.data[self.index] = elem else: self.data.add(elem) self.size += 1 useStack() let h = scan() let w = scan() var isWater : array[3001,array[3001,bool]] for y in 0..= w : continue if ny < 0 or ny >= h : continue if not isWater[nx][ny] : continue X.push(nx) Y.push(ny) isWater[nx][ny] = false return true var ans = 0 for x in 0..