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

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

hide comments
2016-10-02 16:15:47
read the problem carefully. Don't forget to see the output format.
Also easy question no bfs used. Simple recursion. :)
2016-09-27 22:15:37
Nice bfs problem :) AC in one go
2016-09-26 04:35:46
using dfs to find connected components. NZEC in python 2.7.10 but accepted on pypy 2.6

Last edit: 2016-09-26 04:46:38
2016-08-06 21:04:41
focus on array bounds ;gave me 3 runtime errors
finally ac :D
2016-08-06 09:57:32
pheww!!!AC in one go......HAPPY :D :D :D
2016-07-31 14:07:32
don't use find;
int check = mymap.count(ans);
if (check) //key in map
^_^
2016-07-28 20:24:51
ac in 5 GO's

2016-05-17 13:59:18 DEVVRAT GUPTA
map+bfs+lengthy code
2016-05-02 11:02:11
Good application of bfs for a real world image processing problem. My 25th !
2016-02-05 10:24:31 dev
simple bfs +map. Ac in one go!!!
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.