ASCII Island Bipartite Check
You have an ASCII map of an island with different terrain types. Each character represents a terrain type. Terrain regions are connected components of cells with the same character, connected orthogonally (up/down/left/right, not diagonally).
Two regions are adjacent if they share an edge (not just a corner). We can think of the regions as nodes in a graph, with edges between adjacent regions.
Your task: Determine if the region graph is bipartite, meaning we can assign one of two colors to each region such that no two adjacent regions have the same color.
Input
The first line contains r and c (1 ≤ r, c ≤ 300), the dimensions of the map.
The next r lines each contain c characters, representing the terrain map. Characters are lowercase letters a–z.
Output
Print YES if the region graph is bipartite, or NO otherwise.
Notes
Two regions are adjacent if they share an orthogonal edge. A single region may be adjacent to itself indirectly (for example, an a region with a b region between two parts of the a region), but this is considered a normal edge in the graph.
In the second example, there is one region of a and one region of b. They are adjacent, so the graph is bipartite.