#{{{ header import algorithm, sequtils, tables, macros, math, sets, strutils when defined(MYDEBUG): import header proc scanf(formatstr: cstring){.header: "", varargs.} proc getchar(): char {.header: "", varargs.} proc nextInt(): int = scanf("%lld",addr result) proc nextFloat(): float = scanf("%lf",addr result) proc nextString(): string = var get = false result = "" while true: var c = getchar() if int(c) > int(' '): get = true result.add(c) else: if get: break get = false template `max=`*(x,y:typed):void = x = max(x,y) template `min=`*(x,y:typed):void = x = min(x,y) template inf(T): untyped = ((T(1) shl T(sizeof(T)*8-2)) - 1) #}}} import heapqueue proc main():void = let H, W = nextInt() let a = newSeqWith(H, nextString()) var g = newSeqWith(H, newSeqWith(W, 0)) for i in 0.. 0: let t = q.pop() let (x, y) = t[1] for i in 0..<4: let (x2, y2) = (x + vx[i], y + vy[i]) if not inner(x2, y2): continue let d2 = dist[x][y] + g[x2][y2] + 1 if d2 < dist[x2][y2]: dist[x2][y2] = d2 q.push((d2, (x2, y2))) echo dist[H-1][W-1] discard main()