The correspondence between (x,y) and (i,j) should be checked. There are places where dx and dy are confused, probably the indices are misused in places as well.
Check if the array layout good for PNG writer (i.e. the area is not rotated 90 degrees).
Read initial conditions from the file and move the I/O into the separate file to make the working file smaller.