Stars Drawing (Hard Edition)
Contest: Codeforces Round 501 (Div. 3)
Difficulty rating: 1900
Tags: binary search, dp, greedy

A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $$$0$$$ are not allowed).

Let's consider empty cells are denoted by '.', then the following figures are stars:

The leftmost figure is a star of size $$$1$$$, the middle figure is a star of size $$$2$$$ and the rightmost figure is a star of size $$$3$$$.

You are given a rectangular grid of size $$$n \times m$$$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $$$1$$$ to $$$n$$$, columns are numbered from $$$1$$$ to $$$m$$$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $$$n \cdot m$$$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.

In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $$$n \cdot m$$$ stars.

Input format:
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 1000$$$) — the sizes of the given grid.

The next $$$n$$$ lines contains $$$m$$$ characters each, the $$$i$$$-th line describes the $$$i$$$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.

Output format:
If it is impossible to draw the given grid using stars only, print "-1".

Otherwise in the first line print one integer $$$k$$$ ($$$0 \le k \le n \cdot m$$$) — the number of stars needed to draw the given grid. The next $$$k$$$ lines should contain three integers each — $$$x_j$$$, $$$y_j$$$ and $$$s_j$$$, where $$$x_j$$$ is the row index of the central star character, $$$y_j$$$ is the column index of the central star character and $$$s_j$$$ is the size of the star. Each star should be completely inside the grid.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first example the output

is also correct.

Example input:
6 8
....*...
...**...
..*****.
...**...
....*...
........

Example output:
3
3 4 1
3 5 2
3 5 1

Example input:
5 5
.*...
****.
.****
..**.
.....

Example output:
3
2 2 1
3 3 1
3 4 1

Editorial:
I am sorry that some $$$O(n^3)$$$ solutions pass tests in this problem also. I was supposed to increase constraints or decrease time limit.
The general idea of this problem is the same as in the previous problem. But now we should do all what we were doing earlier faster. The solution is divided by two parts.
The first part. Let's calculate four matrices of size $$$n \times m$$$ — $$$up$$$, $$$down$$$, $$$left$$$ and $$$right$$$. $$$up_{i, j}$$$ will denote the distance to the nearest dot character to the top from the current position. The same, $$$down_{i, j}$$$ will denote the distance to the nearest dot character to the bottom from the current position, $$$left_{i, j}$$$ — to the left and $$$right_{i, j}$$$ — to the right. We can calculate all these matrices in $$$O(n^2)$$$ using easy dynamic programming. If we will iterate over all possible $$$i$$$ from $$$1$$$ to $$$n$$$ and $$$j$$$ from $$$1$$$ to $$$m$$$, we can easy see the next: if the current character is dot, then $$$up_{i, j} = left_{i, j} = 0$$$. Otherwise if $$$i > 1$$$ then $$$up_{i, j} = up_{i - 1, j}$$$, and if $$$j > 1$$$ then $$$left_{i, j} = left_{i, j - 1}$$$. Rest two matrices can be calculated the as well as these two matrices but we should iterate over all $$$i$$$ from $$$n$$$ to $$$1$$$ and $$$j$$$ from $$$m$$$ to $$$1$$$. So, this part of the solution works in $$$O(n^2)$$$.
After calculating all these matrices the maximum possible length of rays of the star with center in position $$$(i, j)$$$ is $$$min(up_{i, j}, down_{i, j}, left_{i, j}, right_{i, j}) - 1$$$.
The second part is to draw all stars in $$$O(n^2)$$$. Let's calculate another two matrices of size $$$n \times m$$$ — $$$h$$$ and $$$v$$$. Let's iterate over all stars in our answer. Let the center of the current star is $$$(i, j)$$$ and its size is $$$s$$$. Let's increase $$$h_{i, j - s}$$$ by one and decrease $$$h_{i, j + s + 1}$$$ by one (if $$$j + s + 1 \le m$$$). The same with the matrix $$$v$$$. Increase $$$v_{i - s, j}$$$ and decrease $$$v_{i + s + 1, j}$$$ (if $$$i + s + 1 \le n$$$). Then let's iterate over all possible $$$i$$$ from $$$1$$$ to $$$n$$$ and $$$j$$$ from $$$1$$$ to $$$m$$$. If $$$i > 1$$$ then set $$$v_{i, j} := v_{i, j} + v_{i - 1, j}$$$ and if $$$j > 1$$$ set $$$h_{i, j} := h_{i, j} + h_{i, j - 1}$$$.
How to know that the character at the position $$$(i, j)$$$ is asterisk character or dot character? If either $$$h_{i, j}$$$ or $$$v_{i, j}$$$ greater than zero, then the character at the position $$$(i, j)$$$ in our matrix will be the asterisk character. Otherwise it is the dot character. This part works also in $$$O(n^2)$$$.
Time complexity of the solution: $$$O(n^2)$$$.