calculateGridDimensions