Label each tree square with its edge-step distance to the nearest empty square or the outside of the grid and print the dot-padded grid.
Medium4BFSMatrixImplementationInterviewNo attempts yetTime limit1sMemory limit256 MBDee Siduous is a botanist who studies trees. Much of her research is about how tree rings form and what they say about the growing conditions over a tree's lifetime. She wants to run simulations to check her theory against the data she gathered in the field.
One thing she has to compute is the number of rings implied by the outline of a tree. Dee models a cross section of a tree on a two dimensional grid and marks the interior of the tree as a set of grid squares. Every square that is not part of the tree is ring 0, and so is every square outside the grid. For n≥1, ring n is made up of all the squares that have no number yet and that share an edge with at least one ring n−1 square.
The tree squares can form any shape. They may be split into separate pieces, and they may enclose empty holes.
The figure below is an example. The left grid is the input, the right grid holds the ring number of every square.

Given the grid, print the ring number of every square.
The first line contains two positive integers n and m, the number of rows and the number of columns of the tree grid, where n,m≤100.
Each of the next n lines contains m characters. T marks a square of the tree and . marks a square that is not part of the tree.
Print the grid of ring numbers. If the largest ring number is less than 10, use two characters for each grid square, otherwise use three characters for each grid square.
Right justify the ring number inside its square and fill the remaining characters with .. A square that is not part of the tree is filled entirely with .. Print every row and every column, including the ones that hold no ring number at all.