In a NxN array of bool values that represents a bitmap of a monochrome image, I need to find its rectangular boundaries (TopLeft, TopRight, BottomLeft, BottomRight coordinates). There is only one "image" in that NxN array and it's "continuous" - every next pixel is located at x+1 or x-1 or y+1 or y-1 relative to its neighbor.
The simplest algorithm I came up with is just to find first (upper-left) pixel (x,y) and then recursively probe four adjacent pixels (x+1, y), (x-1, y), (x, y+1), (x, y-1) and advance if any found..
Can you suggest something more efficient?
[EDIT] I'm looking for an algorithm that would be more efficient that a full scan. I.e. the boundary should be found with a minimal number of steps.