Arson In Berland Forest
Contest: Technocup 2020 - Elimination Round 3
Difficulty rating: 2200
Tags: binary search, graphs, graphs, shortest paths

The Berland Forest can be represented as an infinite cell plane. Every cell contains a tree. That is, contained before the recent events.

A destructive fire raged through the Forest, and several trees were damaged by it. Precisely speaking, you have a $$$n \times m$$$ rectangle map which represents the damaged part of the Forest. The damaged trees were marked as "X" while the remaining ones were marked as ".". You are sure that all burnt trees are shown on the map. All the trees outside the map are undamaged.

The firemen quickly extinguished the fire, and now they are investigating the cause of it. The main version is that there was an arson: at some moment of time (let's consider it as $$$0$$$) some trees were set on fire. At the beginning of minute $$$0$$$, only the trees that were set on fire initially were burning. At the end of each minute, the fire spread from every burning tree to each of $$$8$$$ neighboring trees. At the beginning of minute $$$T$$$, the fire was extinguished.

The firemen want to find the arsonists as quickly as possible. The problem is, they know neither the value of $$$T$$$ (how long the fire has been raging) nor the coordinates of the trees that were initially set on fire. They want you to find the maximum value of $$$T$$$ (to know how far could the arsonists escape) and a possible set of trees that could be initially set on fire.

Note that you'd like to maximize value $$$T$$$ but the set of trees can be arbitrary.

Input format:
The first line contains two integer $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^6$$$, $$$1 \le n \cdot m \le 10^6$$$) — the sizes of the map.

Next $$$n$$$ lines contain the map. The $$$i$$$-th line corresponds to the $$$i$$$-th row of the map and contains $$$m$$$-character string. The $$$j$$$-th character of the $$$i$$$-th string is "X" if the corresponding tree is burnt and "." otherwise.

It's guaranteed that the map contains at least one "X".

Output format:
In the first line print the single integer $$$T$$$ — the maximum time the Forest was on fire. In the next $$$n$$$ lines print the certificate: the map ($$$n \times m$$$ rectangle) where the trees that were set on fire are marked as "X" and all other trees are marked as ".".

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Example input:
3 6
XXXXXX
XXXXXX
XXXXXX

Example output:
1
......
.X.XX.
......

Example input:
10 10
.XXXXXX...
.XXXXXX...
.XXXXXX...
.XXXXXX...
.XXXXXXXX.
...XXXXXX.
...XXXXXX.
...XXXXXX.
...XXXXXX.
..........

Example output:
2
..........
..........
...XX.....
..........
..........
..........
.....XX...
..........
..........
..........

Editorial:
Let's note that if there is a possible configuration in which the forest burnt $$$T$$$ minutes then there is a configuration when the forest burnt $$$T - 1$$$ minutes. So we can binary search the answer.
Now we need to check the existence of the configuration for a fixed time $$$T$$$. Let's find all trees that can be set on fire. There are two equivalent conditions for such trees: either the square of length $$$2T + 1$$$ with a center in this cell contains only X-s or a distance between the current cell and any cell with "." (or border) is more or equal to $$$T$$$.
We can use any of the conditions. The first condition can be checked with prefix sums on 2D - we can precalculate them one time and use them to take a sum on a rectangle. The second condition can be checked by running bfs from all "."-s or borders (or from X-s which are neighboring to "."-s or to the borders) also one time before the binary search.
The second step is to check that it's possible to cover all burnt trees starting from all set-on-fire trees. We can check it either with "add value on a rectangle" (using prefix sums) since each set-on-fire tree will burn a $$$(2T + 1) \times (2T + 1)$$$ square with center in it. Or, alternatively, we can run bfs from set-on-fire trees.
Anyways, both algorithms have $$$O(nm)$$$ complexity. And, since all damaged trees are shown on the map, the answer can't be more than $$$\min(n, m)$$$. So, the total complexity is $$$O(nm \log(\min(n, m)))$$$.