Hi Saif,
sorry for the delay. The way you constructed the cubes, from a single plane, caused them to all have their Origins in the same place. So if you want the Z location to be a function of the X and Y location of the objects, that Z location will be the same for all the cubes.
Select all the cubes and go to Object > Set Origin > Origin to Geometry.