UCV2013H - Slick


A maritime accident has caused oil to spill onto the seas of Felipistonia, which is a major natural disaster. The Felipistonia's government wants to clean up this mess before more damage occurs. To do this, they first have to know how serious was the accident and the amount of oil that has been spilled into the sea. The only instrument the Felipistonia's government has to get information of the magnitude of this disaster, is the use of satellite images. With these images they can estimate how much money they have to spend to clean this mess. For this, the number of slicks in the seas and the size of each slick must be know. A slick is a patch of oil floating on water. Unfortunately, the Felipistonia's people are not very bright, so they have hired you to help them process the image.

An example of an image obtained by the satellites is shown in Figure 1(a). This image can be transformed to 0's and 1's as seen in Figure 1(b). Given this binary matrix, your job is to count the number of slicks in the ocean and their corresponding size. Two adjacent pixels in the image are considered to be in the same slick if they are in the same row or the same column.

(a) A satellite image of the spilled oil. (b) The representation of the image in a binary matrix

Figure 1: (a) A satellite image of the spilled oil. (b) The representation of the image in a binary matrix

Input

The input contains several test cases, each one corresponding to a different satellite image. The first line of each case contains two integers that indicate the number of rows (N) and columns (M) in the image (1 <= N, M <= 250). Then N lines follows with M integers each, containing the information of the image.

The end of input is indicated by a test case with N = M = 0. This case should not be processed.

Output

For each image, output the number of slicks in the sea. Additionally, output the size of each slick in ascending order and the number of slicks of that size.

Example

Input:
10 10
1 1 1 1 1 1 1 1 1 1
1 1 1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 1 1 1
1 1 0 0 1 0 0 1 1 1
1 0 1 0 0 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
0 0

Output:
7
1 2
2 1
6 1
10 2
20 1

hide comments
candide: 2018-10-22 02:04:38

Weak tests.

sachinspoj: 2018-10-08 05:03:48

Easy Peacy :)
AC in one go! 0.00s

Last edit: 2018-10-08 05:06:54
vivek_prime: 2018-01-19 15:35:09

ac in one go.
just do a bfs and it would be better if u consider a adjacency matrix. take set to keep unique values as the final ans

sirjan13: 2017-12-03 22:21:50

easy one Dfs :)

sonorous: 2017-10-29 07:37:02

AC in one go!!! DFS with lookup table!!! similar to flood fill !

d_skyhawk: 2017-03-23 12:16:33

AC in 1 GO

vladimira: 2017-03-05 13:46:46

easy concept, but a lot of time spent coding, so implementaion is little underestimated

vengatesh15: 2017-03-04 06:34:55

easy one ...

gboduljak: 2017-02-23 22:49:53

this could be also categorized as flood_fill(dfs) problem, find connected components basically

abhishekrahul: 2016-12-20 10:01:16

AC in one go! easy one , you could also solve sheep(koze) after this !!!!!


Added by:Hector Navarro
Date:2013-07-22
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Local UCV 2013. Francisco Sans