## APL, <s>53 49 43 42 40</s> 39 I managed to replicate J's `;.` in APL, and used [Gareth's approach](http://codegolf.stackexchange.com/a/12444/7911), saving 10 chars. {5⌷⍵+(+/⍵)×0=5⌷⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1 Sample run: {5⌷⍵+(+/⍵)×0=5⌷⍵}¨3,⌿3,/×∘?∘9¨∘.∨⍨9⍴0 1 4 8 4 4 6 3 1 9 49 7 45 6 46 8 3 8 6 4 8 5 9 7 41 4 36 7 49 8 8 4 1 2 4 1 7 8 37 6 29 8 37 2 7 2 1 2 5 7 3 --- Explanation: - `∘.∨⍨9⍴0 1` generates a bit mask. - `×∘?∘9¨` multiplies each bit by a random value from 1 to 9 inclusive, generating a masked grid of random numbers. - `3,⌿3,/` uses what can only be described as hackery to return all 3 by 3 overlapping boxes in the masked array. These are also flattened in the process. - `{5⌷⍵+(+/⍵)×0=5⌷⍵}¨` iterates over the array, assigning each element to `⍵`. For each iteration, it does the following: - `0=5⌷⍵` takes the fifth (middle, remembering that APL indexing is 1-based), and compares it with 0. It returns 1 if true, and 0 if not. - `(+/⍵)×` multiplies the result with the sum of the elements in the box. - `5⌷⍵+` then re-adds the middle element to the result.