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-----

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-----

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 ".".


-----Examples-----
Input
3 6
XXXXXX
XXXXXX
XXXXXX

Output
1
......
.X.XX.
......

Input
10 10
.XXXXXX...
.XXXXXX...
.XXXXXX...
.XXXXXX...
.XXXXXXXX.
...XXXXXX.
...XXXXXX.
...XXXXXX.
...XXXXXX.
..........

Output
2
..........
..........
...XX.....
..........
..........
..........
.....XX...
..........
..........
..........

Input
4 5
X....
..XXX
..XXX
..XXX

Output
0
X....
..XXX
..XXX
..XXX

Source: codeforces | Difficulty: HARD
Tags: Sorting

Graph algorithms

Data structures

Shortest paths


Solutions:
import sys
input = lambda : sys.stdin.readline().rstrip()
(h, w) = map(int, input().split())
d = w + 2
b = [0] * (w + 2)
for i in range(h):
	b += [0] + [int(j == 'X') for j in input()] + [0]
b += [0] * (w + 2)
co = sum(b)
t = 10 ** 10
for i in range(1, h + 1):
	f = 0
	for j in range(w + 2):
		f += b[i * d + j]
		if b[i * d + j] == 0 and f:
			t = min(t, (f - 1) // 2)
			f = 0
for j in range(1, w + 1):
	f = 0
	for i in range(h + 2):
		f += b[i * d + j]
		if b[i * d + j] == 0 and f:
			t = min(t, (f - 1) // 2)
			f = 0
coo = 0
ans = [-1] * (w + 2) * (h + 2)
stack = []
for i in range(h + 2):
	for j in range(w + 2):
		if b[i * d + j] == 0:
			stack.append(i * d + j)
			ans[i * d + j] = 0
dij = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
for ij in stack:
	(i, j) = divmod(ij, d)
	for (x, y) in dij:
		if 0 <= i + x < h + 2 and 0 <= j + y < w + 2:
			if ans[(i + x) * d + j + y] == -1:
				ans[(i + x) * d + j + y] = ans[i * d + j] + 1
				if ans[(i + x) * d + j + y] > t:
					coo += 1
				stack.append((i + x) * d + (j + y))
if coo * 6 < co and t == 1:
	t -= 1
print(t)
for i in range(1, h + 1):
	print(''.join(['.X'[int(t < j)] for j in ans[i * d + 1:i * d + w + 1]]))