A BFS combined with floodfill should be enough.
Basically, you do the following:
Iterate until you find the first X
Run a BFS on it and change all pixels to visisted = True. Also keep track if you happen to reach the boundary or fulfill any other criterion s.t. it is disqualified
If it is not disqualified, save its size as the current max-size