Island Buses

No attempts yetTime limit2sMemory limit256 MB

Problem

A shipping company plans bus service for several South Pacific island nations. A nation is a set of islands, and some of them are joined by bridges. To hold costs down the company runs as few buses as it can. Every island must have access to a bus, but a group of two or more islands joined by bridges gets only one bus.

Given the map of an island nation, compute the number of islands, the number of bridges, and the smallest number of buses that nation needs.

Input

The input holds a sequence of maps. Each map is a rectangular grid of characters with at most 80 rows and at most 80 columns. One blank line separates two consecutive maps. The input ends at end of file.

A map contains only the four characters ., X, # and B. A dot is ocean water. X and # are island land. B is bridge, and every X is the land at which one or more bridges end.

Each map holds one or more islands, and every island is a rectangle of land. Two different islands never touch vertically or horizontally. Diagonal contact is not enough to drive a bus across.

Each map holds zero or more bridges, each of them horizontal or vertical. One bridge is one or more B cells in a straight line, and it joins only the two islands that own the X cells at its two ends. A B cell that happens to sit beside the # land of some other island does not reach that island. Bridges do not cross each other and do not run over the # land of an island. No B is next to another B or to an X unless both belong to the same bridge.

Output

For each map print the map number, then the number of islands, the number of bridges, and the number of buses needed, one per line. Print a blank line between the reports of two consecutive maps. Use this format:

Map <number>
islands: <count>
bridges: <count>
buses needed: <count>