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 Rust (1.15.1) (judge verdict: Wrong Answer)

use std::fmt;
use std::io::stdin;
use std::io::BufRead;
use std::str;
use std::str::FromStr;
use std::vec::Vec;
use std::cmp;

fn main() {
    let stdin = stdin();
    let mut scan = InputScanner::new(stdin.lock(), 256);

    let h:usize = scan.next();
    let w:usize = scan.next();

    let a:Vec<Vec<char>> = (0..h).map(|_| scan.next::<String>().chars().collect()).collect();

    let answer = partition(&a,w,h,a[0][0],0,0,w-1,h-1);

    println!("{}",answer);
}
fn partition(a:&Vec<Vec<char>>,w:usize,h:usize,color:char,left:usize,top:usize,right:usize,bottom:usize) -> usize {
    let mut dx = 1;
    let mut dy = 1;

    let mut x = left;
    let mut y = top;

    while x <= right && y <= bottom {
       if x == right && y == bottom {
           return 0;
       } else if x == right {
           dx = 0;

           if extendable(a,color,left,top,x,y,dx,dy) {
               y += dy;
           } else {
               let r = partition(a,w,h,a[y+dy][left],left,y+dy,x,bottom) + 1;
               //println!("color = {}, left = {}, top = {}, right = {}, bottom = {}",color,left,top,right,bottom);
               //println!("{}",r);
               return r;
           }
       } else if y == bottom {
           dy = 0;

           if extendable(a,color,left,top,x,y,dx,dy) {
               x += dx;
           } else {
               let r = partition(a,w,h,a[top][x+dx],x+dx,top,right,y) + 1;
               //println!("color = {}, left = {}, top = {}, right = {}, bottom = {}",color,left,top,right,bottom);
               //println!("{}",r);
               return r;
           }
       } else {
           if extendable(a,color,left,top,x,y,1,1) {
               y += 1;
               x += 1;
           } else if extendable(a,color,left,top,x,y,1,0) {
               x += 1;
           } else if extendable(a,color,left,top,x,y,0,1) {
               y += 1;
           } else if a[y+1][left] == a[y+1][x+1] {
               let r = cmp::min(
                   partition(a,w,h,a[top][x+1],x+1,top,right,y) + 1,
                   partition(a,w,h,a[y+1][left],left,y+1,right,bottom)) + 1;
               //println!("color = {}, left = {}, top = {}, right = {}, bottom = {}",color,left,top,right,bottom);
               //println!("{}",r);
               return r;
           } else {
               let r = cmp::min(
                   partition(a,w,h,a[y+1][left],left,y+1,x,bottom) + 1,
                   partition(a,w,h,a[top][x+1],x+1,top,right,bottom)) + 1;
               //println!("color = {}, left = {}, top = {}, right = {}, bottom = {}",color,left,top,right,bottom);
               //println!("{}",r);
               return r;
           }
       }
    }

    return 0;
}
fn extendable(a:&Vec<Vec<char>>,color:char,left:usize,top:usize,right:usize,bottom:usize,dx:usize,dy:usize) -> bool {
    if dx > 0 || dy > 0 {
        if dy > 0 {
            for x in left..(right + dx + 1) {
                if a[bottom + dy][x] != color {
                    return false;
                }
            }
        }

        if dx > 0 {
            for y in top..(bottom + dy + 1) {
                if a[y][right + dx] != color {
                    return false;
                }
            }
        }

        return true;
    } else {
        panic!("dx and dy is 0");
    }
}
struct InputScanner<R: BufRead> {
	reader: R,
	buf: Vec<u8>, // Should never be empty
	pos: usize,   // Should never be out of bounds as long as the input ends with '\n'
}
impl<R: BufRead> InputScanner<R> {
	fn new(reader: R, capacity: usize) -> Self {
		InputScanner {
			reader: reader,
			buf: Vec::with_capacity(capacity),
			pos: 0,
		}
	}

	#[inline]
	fn next<T: FromStr>(&mut self) -> T
	where
		T::Err: fmt::Debug,
	{
		if self.buf.is_empty() {
			self._read_next_line();
		}
		let mut start = None;
		loop {
			match (self.buf[self.pos], start.is_some()) {
				(b' ', true) | (b'\n', true) | (b'\r', true) => break,
				(_, true) | (b' ', false) => self.pos += 1,
				(b'\n', false) | (b'\r', false) => self._read_next_line(),
				(_, false) => start = Some(self.pos),
			}
		}
		let target = &self.buf[start.unwrap()..self.pos];
		unsafe { str::from_utf8_unchecked(target) }.parse().unwrap()
	}

	#[inline]
	fn _read_next_line(&mut self) {
		self.pos = 0;
		self.buf.clear();
		if self.reader.read_until(b'\n', &mut self.buf).unwrap() == 0 {
			panic!("Reached EOF");
		}
	}
}