After a successful crowdfunding campaign, Sheba Arriba has raised enough money for her mail-order biology supply company. Sheba's Amoebas ships Petri dishes already populated with a colony of those tiny one-celled organisms. Sheba needs to verify the number of amoebas her company sends out. For each dish she has a black-and-white image that has been pre-processed to show each amoeba as a simple closed loop of black pixels. A loop is a minimal set of black pixels in which each pixel is adjacent to exactly two other pixels in the set, where adjacent means sharing an edge or a corner of a pixel. All black pixels in the image belong to some loop.
Write a program that counts the closed loops in a rectangular array of black and white pixels. No two closed loops in the image touch or overlap. One cannibalistic species of amoeba surrounds and engulfs its neighbors, so there may be amoebas within amoebas. For instance, each of the images in Figure 1 contains four amoebas.

Figure 1: Two Petri dishes, each with four amoebas.