Hide

Problem C
Borders

Consider a black and white image, where every pixel has a value of either 0 or 1. Define a region of the image as a collection of pixels that all have the same value, and are interconnected. Specifically, for any two pixels in a region, there is a path between them only going up, down, left or right, and only going through other pixels with the same value.

You wish to see a border completely around every region in the image. You can choose certain regions to draw a border around; when you do, you draw a border around the entire region, including any internal “holes” (regions entirely contained within the region). If two regions are adjacent, then you can supply the border between then by drawing the border around one, or the other, or both. What is the minimum number of regions you need to draw a border around in order to ensure that every region has a border?

Consider these examples:

\includegraphics[width=1.0\textwidth ]{borders.png}
  • In the first case, the minimum is three. Because they’re on the edges, there is no choice but to draw a border around all three.

  • In the second case, the minimum is one. Drawing a border around the 0 region puts a border around the 1 region.

  • In the third case, the answer is eight. Drawing a border around all of the regions on the edges puts a border around the center region.


Input

The first line of input contains two integers $n$ and $m$ ($1 \le n,m \le 100$), where $n$ is the number of rows in the image, and $m$ is the number of columns.

Each of the next $n$ lines contains a single string of length $m$, consisting only of the characters ‘0’ and ‘1’. This is the image.

Output

Output a single integer, which is the minimum number of regions you must draw a border around to ensure that every region has a border.

Sample Input 1 Sample Output 1
3 3
000
111
000
3
Sample Input 2 Sample Output 2
3 3
000
010
000
1
Sample Input 3 Sample Output 3
3 3
010
101
010
8

Please log in to submit a solution to this problem

Log in