Fix condition for closed contours

The original code assumes that if a contour line returns to its starting
cell, it is a closed contour.  That only holds if the cell has a single
contour crossing.  This fix adds the condition that a closed contour
must end on the same edge.
Add test to verify closed contour condition