import sequtils,math,strutils # template stopwatch(body) = (let t1 = cpuTime();body;stderr.writeLine "TIME:",(cpuTime() - t1) * 1000,"ms") # 行列 type Matrix[T] = ref object w,h:int data: seq[T] proc `[]`[T](m:Matrix[T],x,y:int):T = m.data[x + y * m.w] proc `[]`[T](m:var Matrix[T],x,y:int):var T = m.data[x + y * m.w] proc `[]=`[T](m:var Matrix[T],x,y:int,val:T) = m.data[x + y * m.w] = val proc newMatrix[T](w,h:int):Matrix[T] = new(result) result.w = w result.h = h result.data = newSeq[T](w * h) proc newMatrix[T](arr:seq[seq[T]]):Matrix[T] = new(result) result.w = arr[0].len result.h = arr.len result.data = newSeq[T](result.w * result.h) for x in 0.." .} proc scan(): int = while true: let k = getchar_unlocked() if k < '0': return result = 10 * result + k.ord - '0'.ord let h = scan() let w = scan() proc encode(x,y:int):int = x * h + y let M = (proc ():Matrix[bool] = result = newMatrix[bool](w*h,w*h) const dxdy9 :seq[tuple[x,y:int]] = @[(0,1),(1,0),(0,-1),(-1,0),(1,1),(1,-1),(-1,-1),(-1,1),(0,0)] for x in 0..= w or ny >= h : continue result[encode(x,y),encode(nx,ny)] = true )() var C = newSeq[bool](w*h) for y in 0..