#P15888. [COCI 2025/2026 #6] Čokolada
[COCI 2025/2026 #6] Čokolada
Description
Luka loves chocolate. He is very excited to eat a large chocolate bar made of rows and columns he got for his birthday. The chocolate consists of black and white squares of chocolate. However, Luka doesn’t really like white chocolate and would only like to eat the black squares.
Before he starts eating, Luka will cut the chocolate. He will make a number of vertical and/or horizontal cuts in between the rows and the columns of the chocolate. A vertical cut goes from the top edge of the chocolate to the bottom edge, while a horizontal cut goes from the left edge to the right edge. After making the cuts, Luka will obtain several rectangular pieces of chocolate.
Since Luka will only eat the black parts of the chocolate, he wants to completely separate the black squares from the white squares. This means that every resulting piece must consist entirely of black chocolate or entirely of white chocolate.
Luka would rather start eating as soon as possible instead of wasting time cutting so he asks you to help him determine the minimum number of cuts required to separate the black squares from the white squares.
:::align{center}
:::
Slika 1: This is how the chocolate from the first example should be cut in order to separate the black parts from the white parts with minimum number of cuts
Input Format
In the first line, there are two natural numbers and (), representing the number of rows and columns of the chocolate.
Each of the following lines contains characters, each of which is either or . The character denotes a white square, and denotes a black square.
Output Format
In the first and only line, output a single number - the minimum number of cuts required to separate the black squares from the white squares.
4 7
0000000
0111000
0111100
0000000
6
4 5
00000
01100
01100
00000
4
4 4
0101
1010
0101
1010
6
Hint
Clarification of the first example:
Explained in the drawing.
Clarification of the second example:
Luka should make a cut between the first and second row, third and fourth row, first and second column, and third and fourth column.
Clarification of the third example:
Luka should make a cut between every two adjacent rows and columns - a total of cuts.
| Subtask | Points | Constraints |
|---|---|---|
| 1 | 5 | The chocolate is a chessboard, i.e. the square in the -th row and -th column is white if is an even number, and black if it is odd. |
| 2 | 11 | |
| 3 | Exactly one square is black. | |
| 4 | 23 | No additional constraints. |
京公网安备 11011102002149号