fixed out of range issue with Terrain when using gradient, and simplified the code by using enumerate2d.