Optimize graph generation for 2D grid - C#