The problem is that you are modifying the grid in the same pass as you are evaluating what needs to change. Each time you change a cell, you are affecting the outcome of all future tests in the same pass that border that cell.
You need to make a copy of the grid. Always test (read) from that copy, and apply changes (write) to the original.