Optimize grid margin