Optimal classical and quantum real and complex dimension witness