Island
Time limit5sMemory limit768 MB
Label each island in a grid as safe (O) or dangerous (X), where an island is dangerous if reaching the outer sea requires crossing another island.
Problem
A tube flying his private plane over the middle of the Pacific Ocean ran out of fuel and made an emergency landing in the middle of a remote island. Luckily, the tube found a grid map that contains all the information about the area near the island.
Each cell of the grid is either sea or land. All cells on the outermost border are sea. A cell is written as (, ), and any cell with , , , or is guaranteed to be sea. Every land cell is part of some island. Two land cells that share an edge belong to the same island. In other words, if and are both land, they belong to the same island, and if and are both land, they also belong to the same island.
The tube picked up a raft at its current position and plans to use it to cross the open ocean in search of fuel. The tube can move from any cell to one of its four adjacent cells. Whether the destination is land or sea does not matter, because the tube has a raft. The tube's goal is to move from its current position, reach the open sea, find fuel, and come back.
The tube faces two problems. First, one of the islands in the area is home to dangerous hunters who want to catch and eat ducks. The island the tube is on is known to be safe, but if escaping requires passing through the hunters' island, the tube is in serious danger. Second, the tube does not know which island on the map it is on. The island it is on might border the outermost sea directly, or it might be surrounded by other islands in layers.
The tube wants to know whether each island on the map is safe or dangerous. An island is dangerous if there is another island that must be passed through to reach an outermost cell from . An island is safe if it is not dangerous. Help the tube find which islands are safe and which are dangerous.
Input
The first line contains two integers and separated by a space. ()
The next lines each contain a string of length . The -th character of the -th line is # if cell is land and . if it is sea. No other characters appear. The grid contains at least one land cell.
Output
Print lines, each containing a string of length . The -th character of the -th line describes cell . Print . for sea, O for a cell of a safe island, and X for a cell of a dangerous island.