#P5962. Ship Tonnage Counting

    ID: 19187 Type: Default 1000ms 256MiB

Ship Tonnage Counting

Ship Tonnage Counting

You are given an n×nn \times n grid representing a board for a ship game. Each cell is either black (denoted by 'B', representing a part of a ship) or empty (denoted by '.'). Two black cells that share a common edge belong to the same ship. Different ships do not touch each other along their sides. The tonnage of a ship is defined as the number of connected black cells.

For example, in the sample illustration, the board contains one 2929-ton ship, three 77-ton ships, two 44-ton ships, and three 11-ton ships. Your task is to determine the tonnage and the number of ships corresponding to that tonnage for a given board.

inputFormat

The first line contains an integer nn (1n10001 \le n \le 1000), representing the size of the board. The next nn lines each contain exactly nn characters. Each character is either 'B' (indicating a black cell, part of a ship) or '.' (indicating an empty cell).

outputFormat

For each unique ship tonnage, output a line with two space-separated integers: the tonnage and the number of ships with that tonnage. The output should be sorted in descending order of tonnage.

sample

3
B..
.B.
..B
1 3