A generic example has been found: a genus two configuration,
especially a genus 2 with deeply embedded holes. The initial
configuration can be modified. The starting configuration
assumed that right is always valid, but this can lead to
a move right through a region into a hole, around which
the program will cycle endlessly. A solution is two part.
First mark the visited squares with a region number.
This will become a map later to point to a data structure:
region n will have a pointer to a polygon. The outer loop
and inner loop will be added.  The generic example leads to
two cases:

A) At potential starting vertex [r, c].  Direction is RIGHT.

The inside and outside boxes have the same class. You
CANNOT go right without crossing the region.

Point LEFT from upper left corner.

A1) If the inside box has the same class, this cannot be
a starting point.  Mark the box at [r+1,c+1] with the same
region number, and exit. CHECKSUM: the boxes above and left
should be in the same region.

A2) If the inside box has a different class, proceed ccw.
You are now circumscribing an inner ring of the region.