Wheat Harvest
InterviewTime limit1sMemory limit128 MB
Find each connected block of 1s, order blocks by area, and label every cell with its block's rank.
Problem
Zoran lives on a farm and owns the biggest combine harvester in the village. The crop buyout went badly this year, but Zoran still sowed wheat in several places. Harvest time has arrived and he does not feel much like working.
Even so, he harvests one wheat field each day. The field he harvests that day is the smallest one by area among the fields still standing.
Zoran's land is an grid of squares. For every square it is known whether Zoran sowed wheat there. Two squares are adjacent if they share a side.
A wheat field is a maximal set of sown squares connected through adjacency. The left table in the picture below contains four fields. The right table gives each field the number of the day on which Zoran harvests it.

Write a program that determines, for every sown square, the day on which Zoran harvests it.
Input
The first line contains natural numbers and , the size of Zoran's land ().
Each of the next lines contains characters, either '0' or '1'. A '0' is a square without wheat, and a '1' is a square with wheat.
The number of wheat fields is less than 10, and no two wheat fields have the same area.
Output
Print lines of characters each. A square without wheat gets '0', and every other square gets the ordinal number of the day on which that square is harvested.