Breadth First Search


Rectangular bitmap of size n * m is given. Each pixel of the bitmap is either white or black, but at least one is white. The pixel in i-th line and j-th column is called the pixel (i, j). The distance between two pixels p1 = (i1, j1) and p2 = (i2, j2) is defined as:

d(p1, p2) = |i1i2| + |j1j2|

For each pixel find the distance to the nearest white pixel.


The number of test cases t is given in the first line, then t test cases follow. First line of each test case contains pair of integer numbers n, m (1n1000, 1m1000). In each of the following n lines of the test case exactly one zero-one word of length m, the description of one line of the bitmap, is written. On the j-th position in the line i (1in, 1jm) there is 1 if, and only if the pixel (i, j) is white.


In the i-th line for each test case there should be written m integers f(i, 1), ..., f(i, m) separated by single spaces, where f(i, j) is the distance from the pixel (i, j) to the nearest white pixel.

Time limit 3 seconds
Memory limit 128 MiB
Input example #1
3 4
Output example #1
3 2 1 0
2 1 0 0
1 0 0 1