Corrected (x, y) order when flattening a 2D grid