#P6566. Galaxies in the Starry Sky

    ID: 19778 Type: Default 1000ms 256MiB

Galaxies in the Starry Sky

Galaxies in the Starry Sky

Jimmy and Symbol have planned to stargaze together. The night sky is modeled as a matrix of size (N\times M) where each cell with coordinates ((i,j)) (with (1 \le i \le N) and (1 \le j \le M)) may either be empty or contain a star. Two stars are part of the same constellation if they are located in adjacent positions. Adjacent positions include the left, left-up, up, right-up, right, right-down, down, and left-down neighbors. This connectivity is transitive; that is, if star A is connected to star B and star B to star C, then all three stars belong to the same constellation.

A galaxy is defined as all constellations that have an identical number of stars. The size of a galaxy is the total number of stars contained in all of its constellations. Symbol asks Jimmy to determine the number of galaxies in the starry sky and to find the size of the largest galaxy.

Note: If the sky contains no stars, both the number of galaxies and the largest galaxy size should be reported as 0.

inputFormat

The first line contains two space-separated integers (N) and (M), representing the number of rows and columns of the sky. The following (N) lines each contain (M) integers (either 0 or 1) separated by spaces, where 1 indicates a star and 0 indicates an empty position.

outputFormat

Output two integers separated by a space: the number of galaxies, and the size of the largest galaxy.

sample

3 3
1 1 0
1 0 0
0 0 1
2 3