Based on your result vs. the expected one I would guess that your grid indices are flipped.
Did you check how these values are created and made sure that the “(x, y)” coordinates are in the expected image coordinate system (often having the origin in the top left and positive axes to the right and bottom)?