TAMOLS: Terrain-Aware Motion Optimization for Legged Systems