>>909 Squeak Smalltalk

| mat genNeighs neighs pos queue |
mat := Matrix rows: 2 columns: 3 element: -1.
pos := 1@1.
genNeighs := [pos + {(-1@0). (1@0). (0@ -1). (0@1)} select: [:pt |
 (pt x between: 1 and: mat columnCount) and: [pt y between: 1 and: mat rowCount]]].
queue := OrderedCollection with: {pos->mat}.
[queue notEmpty] whileTrue: [
 | prevMat seq |
 seq := queue removeFirst.
 pos := seq last key.
 prevMat := mat := seq last value.
 (mat noneSatisfy: #negative) ifTrue: [^seq allButFirst size].
 neighs := genNeighs value.
 mat indicesDo: [:y :x | (mat at: y at: x) > 0 ifTrue: [mat at: y at: x incrementBy: -1]].
 neighs select: [:pt | (mat at: pt y at: pt x) <= 0]
  thenDo: [:pt | queue add: seq, {pt->mat copy}].
 neighs select: [:pt | (mat at: pt y at: pt x) < 0]
  thenDo: [:pt | queue add: seq, {pos->(mat copy at: pt y at: pt x put: 5; yourself)}].
 prevMat ~= mat ifTrue: [queue add: seq, {pos->mat copy}]].
^-1 "=> 9 "

富豪的すぎるので暇をみてもうちょっとリファインします^^;