The Hilbert basis method for D-flat directions and the superpotential