Spawn of Ungoliant
InterviewTime limit1sMemory limit256 MB
Spiders spread to every tree connected by four-directional adjacency to an infested tree, and isolated trees and open floor stay unchanged.
Problem
The spider children of Ungoliant are spreading through Mirkwood. A giant spider cannot leave the tree it lives in, but it lays eggs in the trees next to that tree. Each egg hatches quickly and grows into another spider, which lays eggs of its own.
You are given a map of Mirkwood marking which trees hold spiders and which do not. Work out what the map looks like once the spiders have spread as far as they can.
In this problem, adjacency means one of the four cardinal directions (north, south, east, west). Two trees that touch only at a diagonal are not adjacent.
Input
The input holds several maps. The first line of a map gives the width and the height of that map. The next lines each hold a string of length describing the layout of Mirkwood. S is a tree with spiders in it, T is a tree with no spiders, and . (a period) is open forest floor with no tree.
A line where and are both 0 ends the input. Do not process that line.
Output
For each map, print the map of Mirkwood once the spiders can spread no further. Print no blank line between maps.