Problem:
Score : 1000 points

Problem Statement

Note the unusual memory limit.

For a rectangular grid where each square is painted white or black, we define its complexity as follows:

If all the squares are black or all the squares are white, the complexity is 0.

Otherwise, divide the grid into two subgrids by a line parallel to one of the sides of the grid, and let c_1 and c_2 be the complexities of the subgrids. There can be multiple ways to perform the division, and let m be the minimum value of \max(c_1, c_2) in those divisions. The complexity of the grid is m+1.

You are given a grid with H horizontal rows and W vertical columns where each square is painted white or black.
HW characters from A_{11} to A_{HW} represent the colors of the squares.
A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

Find the complexity of the given grid.

Constraints

1 \leq H,W \leq 185

A_{ij} is # or ..

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the complexity of the given grid.

Sample Input 1

3 3
...
.##
.##

Sample Output 1

2

Let us divide the grid by the boundary line between the first and second columns.
The subgrid consisting of the first column has the complexity of 0, and the subgrid consisting of the second and third columns has the complexity of 1, so the whole grid has the complexity of at most 2.

Sample Input 2

6 7
.####.#
#....#.
#....#.
#....#.
.####.#
#....##

Sample Output 2

4

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

//package at_coder;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc =  new Scanner(System.in);
		int H = sc.nextInt();
		int W = sc.nextInt();
		String[] board = new String[H];
		for(int i=0;i<H;i++) {
			String line = sc.next();
			board[i] = line;
		}

		sc.close();

		Calc calc = new Calc();
		board = calc.cmprsedBoard(calc.cnvrtedBoard(calc.cmprsedBoard(board)));
/*
		for(int i=0;i<board.length;i++)
			System.out.println(board[i]);
			*/

		System.out.println(calc.log2(board.length)+calc.log2(board[0].length())+1);
	}
}

class Calc {
	int log2(int length) {
		int binary = 1;
		int count = 0;
		while(length>binary) {
			binary = binary << 1;
			count++;
		}
		return count;
	}

	int min(int[] nums) {
		int min = nums[0];
		for(int num:nums)
			if(min > num)
				min = num;
		return min;
	}

	int max(int x,int y) {
		if(x>y)
			return x;
		else
			return y;
	}

	String[] cmprsedBoard(String[] board) {
		int index =0;
		int index_point = 1;
		int length = board.length;
		for(;index_point<length;index_point++) {
			if(board[index].equals(board[index_point])) {

			}else {
				index++;
				board[index] = board[index_point];
			}
		}
		return Arrays.copyOfRange(board,0,index+1);
	}

	String[] cnvrtedBoard(String[] board){
		int height = board.length;
		int width = board[0].length();
		String[] res = new String[width];

		for(int j=0;j<width;j++) {
			String line = "";
			for(int i=0;i<height;i++) {
				line += board[i].charAt(j);
			}
			res[j] = line;
		}
		return res;
	}

	int complexity(String[] board) {
		int height = board.length;
		int width = board[0].length();
		char mark = board[0].charAt(0);
		for(int i=0;i<height;i++) {
			for(int j=0;j<width;j++) {
				if(board[i].charAt(j)!=mark) {
					int[] scores = new int[height+width-2];

					for(int k=1;k<height;k++) {
						scores[k-1] = max(complexity(Arrays.copyOfRange(board,0,k)),
								complexity(Arrays.copyOfRange(board,k,height)));
					}

					String[] cnvBoard = cnvrtedBoard(board);
					for(int k=1;k<width;k++) {
						scores[k+height-2] = max(complexity(Arrays.copyOfRange(cnvBoard,0,k)),
								complexity(Arrays.copyOfRange(cnvBoard,k,width)));
					}

					return min(scores)+1;
				}
			}
		}

		return 0;

	}
}