Moving cubes into a grid formation

How would I go about implementing a scene where a random number of ‘soldier’ cubes (with NavMeshAgents) are spawned and then they move into a grid formation at where the mouse was clicked (depends on how many soldiers were spawned).

Make only of soldier the formation walk towards the target. Let all others walk towards a “virtual” grid that you lay over this “leader”.