Picking terrain is more complicated part of this. To simply get X,Y,Z by raycasting to terrain.
I've made before terrain raycasting, and there is very fast algorithms, explained around the web.
Then lets say you have 16x16 cells (
cellHeight), that is 256 in total.
cellSize is lets say 4 units (meters).
cellX = Math.floor(pickX / cellSize);
cellY = Math.floor(pickZ / cellSize);
cellInd = cellY * cellWidth + cellX;