Fill
Vasylko is a budding programmer who recently came up with the idea of creating a graphic editor for black-and-white images. However, his enthusiasm only lasted long enough to implement a single tool: the fill function.
In the editor, the image is represented as a rectangular grid of M × N cells, where each cell is either black or white. Two cells are considered adjacent if they share a side. A region is defined as the largest group of contiguous cells of the same color, where each cell can be reached from any other cell in the region by moving through adjacent cells.
The fill tool operates by allowing the user to select any cell in the grid, after which the entire region containing that cell is recolored to the opposite color.
Vasylko now wants to find out how to completely erase an image using his editor. An image is considered erased if it is entirely black or entirely white. Your task is to determine the minimum number of fills required to make the given image completely one color.
Input
You are given two natural numbers N and M (1 ≤ N ≤ 100, 1 ≤ M ≤ 100), which represent the number of rows and columns in the grid of the image. Following this, there are N rows of M characters each. In the i-th row and j-th column, a 0 indicates a white cell, and a 1 indicates a black cell.
Output
Output a single integer — the minimum number of fills required to erase the given image.