Counting the Optimal Solutions in Graphical Models