You are overwriting the array in front of you.
For example with this input:
2 1 0 0 0
your program will answer
1 instead of
The reason is, that after you process the top left corner, your array looks like this:
1 1 1 0
Which is correct, but in the same cycle of the top level
you also simulate the bottom left corner, which spreads to the bottom right and finishes the simulation immediately.
One sollution to this is to use two arrays, reading from one, writing to the other, and swap them at the end of each cycle.