Stability of a floating object is determined by a "righting arm": A torque produced by the horizontal offset between the object's center of mass, and the center of buoyancy (The center of mass of the portion underwater).
Sometimes, that torque produces "positive stability" -- When the object tilts into the water, the center of buoyancy shifts in a direction that increases the righting arm force and pushes the object back upright.
On the other hand, other shapes produce "negative stability" -- Tilting the object shifts the center of buoyancy in a direction that reduces the righting arm force, so the object flips.
This is why ships are (roughly) square shaped when looking at a cross section: If you visualize a floating box tilting to the right, more of the right side will be underwater. This produces a righting arm force that turns the box back to the left.
The worst case for stability is a circle, since no matter what angle the shape is at, the righting moment is always zero -- There's no horizontal offset between the center of mass and center of buoyancy, so a circle never has a righting force.
An iceberg is uniform, so its center of buoyancy is always below its center of mass. This makes its stability trickier, as it relies on changes in the shape of the buoyancy to follow the center of mass when disturbed.
A ship is a pendulum, an iceberg is an upright stick balanced on one's nose :)
Some off-shore speedboats have really incredible self-righting because they've been designed to always have positive stability at all angles. One of my favorite videos of this in action: https://www.youtube.com/watch?v=Y2i1fOJ-itw
The trick is to draw a low-mass object with high displacement, which happens to be possible because of the existence of, uh, anti-iceberg? that has negative mass.
I like https://en.wikipedia.org/wiki/Simulated_annealing it does not guaranty to find the absolute minimum, it has a few parameters that are difficult to tweak, but I got good results.