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: Time Limit Exceeded)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        h: usize,
        w: usize,
        a: [chars; h],
    }

    let mut tbl = vec![vec![vec![vec![-1; h + 1]; w + 1]; h + 1]; w + 1];

    let ans = solve(0, 0, w, h, &a, &mut tbl);

    println!("{}", max(1, ans) - 1);
}

use std::cmp::{max, min};

fn solve(
    x1: usize,
    y1: usize,
    x2: usize,
    y2: usize,
    a: &Vec<Vec<char>>,
    tbl: &mut Vec<Vec<Vec<Vec<i8>>>>,
) -> i8 {
    if tbl[x1][y1][x2][y2] >= 0 {
        return tbl[x1][y1][x2][y2];
    }

    if x1 + 1 == x2 && y1 + 1 == y2 {
        return if a[y1][x1] == '.' { 0 } else { 1 };
    }

    let mut ret = 100;

    for xm in x1 + 1..x2 {
        let cur = {
            let c1 = solve(x1, y1, xm, y2, a, tbl);
            let c2 = solve(xm, y1, x2, y2, a, tbl);

            if c1 <= 1 && c2 <= 1 {
                if c1 == c2 {
                    c1
                } else {
                    2
                }
            } else {
                max(c1, c2) + 1
            }
        };

        ret = min(ret, cur);
    }

    for ym in y1 + 1..y2 {
        let cur = {
            let c1 = solve(x1, y1, x2, ym, a, tbl);
            let c2 = solve(x1, ym, x2, y2, a, tbl);

            if c1 <= 1 && c2 <= 1 {
                if c1 == c2 {
                    c1
                } else {
                    2
                }
            } else {
                max(c1, c2) + 1
            }
        };

        ret = min(ret, cur);
    }

    // dbg!((x1, y1, x2, y2, ret));

    tbl[x1][y1][x2][y2] = ret;
    ret
}