A rectangular board is divided into unit squares. Every square is white at the start. You want to paint some of the squares black so that the board matches a given picture.
One painting operation picks consecutive white squares inside a single row or a single column and paints all of them black. Every picked square has to be white at that moment. A stroke therefore cannot run across a square that is already black, and it cannot touch a square that has to stay white.
Write a program that finds the smallest number of painting operations needed to produce the picture.