Fax 1: SDDSDDSSSDDDSDD
Fax 2: DDDDDDDSSDDDDSSSDSDSSSDSSDSSSSSSDSSSSSSSSSSSSSDSSSDSDDDS
Fax 3: DSDSDSDSDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
Fax 1: 1S 2D 1S 2D 3S 3D 1S 2D
Fax 2: 0S 7D 2S 4D 3S 1D 1S 1D 3S 1D 2S 1D 6S 1D 13S 1D 3S 1D 1S 3D 1S
Fax 3: 0S 1D 1S 1D 1S 1D 1S 1D 1S 56D
Fax 1: 1 2 1 2 3 3 1 2
Fax 2: 0 7 2 4 3 1 1 1 3 1 2 1 6 1 13 1 3 1 1 3 1
Fax 3: 0 1 1 1 1 1 1 1 1 56
There are from one to 24 data sets, followed by a final line containing only -1. A data set starts with a line containing three integers w, r, and g: the width of the fax in pixels, the total number of runs, and the number of run lengths grouped on one line, respectively. All three numbers are positive: w <= 1,000,000,000, r <= 1000, and g <= 40. The rest of the dataset consists of r run lengths, with a new line starting after each group of g run lengths. The last line (possibly the only line) of run lengths may contain fewer than g run lengths. The numbers on each line are blank separated. The first run length may be 0. All others run lengths are positive. No run length may be greater than 1,000,000,000. The total number of pixels in each fax will be a multiple of w, so the pixels form a rectangle. Though commas are shown in the long numbers above for human readability, the integers in the input and output files include no commas.
For each dataset the output contains a line with one integer: the number of components in the fax. No fax encoded in the input will have more than 1,000,000,000 components. Caution: a solution that examines each pixel individually will not finish within the one-minute time limit.
5 8 4 1 2 1 2 3 3 1 2 7 21 8 0 7 2 4 3 1 1 1 3 1 2 1 6 1 13 1 3 1 1 3 1 8 10 10 0 1 1 1 1 1 1 1 1 56 -1
2 3 32