This script
The Lua API already has functions to detect neighboring particles and either return them in a table with simulation.partNeighbors() or as an iterator with simulation.neighbors().
So either you could change the first line of your Update() function to:
local tab = sim.partNeighbors(x, y, 1, 1)
Or you could delete the first line completely and change the for loop to:
for b in sim.neighbors(x, y, 1, 1) do