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: Accepted)

//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

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

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

// ここまで

use std::cmp::max;

fn count(cnt: &Vec<Vec<usize>>, a: usize, b: usize, c: usize, d: usize) -> usize {
    cnt[a][b] - cnt[c][b] - cnt[a][d] + cnt[c][d]
}

fn run() {
    input! {
        h: usize,
        w: usize,
        s: [chars; h],
    }
    let mut cnt = vec![vec![0; w + 1]; h + 1];
    for i in (0..h).rev() {
        for j in (0..w).rev() {
            cnt[i][j] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i + 1][j + 1];
            if s[i][j] == '#' {
                cnt[i][j] += 1;
            }
        }
    }
    let mut dp_h = vec![vec![vec![0; w + 1]; w + 1]; h + 1];
    let mut dp_w = vec![vec![vec![0; h + 1]; h + 1]; w + 1];
    for i in (0..h).rev() {
        for l in 0..w {
            for r in (l + 1)..(w + 1) {
                let c = count(&cnt, i, l, i + 1, r);
                dp_h[i][l][r] = if c == 0 || c == r - l {1} else {0};
                if i + 1 < h && dp_h[i][l][r] == 1 {
                    let c = count(&cnt, i, l, i + 2, r);
                    if c == 0 || c == 2 * (r - l) {
                        dp_h[i][l][r] += dp_h[i + 1][l][r];
                    }
                }
            }
        }
    }
    for i in (0..w).rev() {
        for l in 0..h {
            for r in (l + 1)..(h + 1) {
                let c = count(&cnt, l, i, r, i + 1);
                dp_w[i][l][r] = if c == 0 || c == r - l {1} else {0};
                if i + 1 < w && dp_w[i][l][r] == 1 {
                    let c = count(&cnt, l, i, r, i + 2);
                    if c == 0 || c == 2 * (r - l) {
                        dp_w[i][l][r] += dp_w[i + 1][l][r];
                    }
                }
            }
        }
    }
    let mut ans = 0;
    while dp_h[0][0][w] < h {
        ans += 1;
        let mut next_h = dp_h.clone();
        let mut next_w = dp_w.clone();
        for i in 0..h {
            for l in 0..w {
                for r in (l + 1)..(w + 1) {
                    let k = i + dp_h[i][l][r];
                    let v = dp_h[i][l][r] + dp_h[k][l][r];
                    next_h[i][l][r] = max(next_h[i][l][r], v);
                    next_w[l][i][i + v] = max(next_w[l][i][i + v], r - l);
                }
            }
        }
        for i in 0..w {
            for l in 0..h {
                for r in (l + 1)..(h + 1) {
                    let k = i + dp_w[i][l][r];
                    let v = dp_w[i][l][r] + dp_w[k][l][r];
                    next_w[i][l][r] = max(next_w[i][l][r], v);
                    next_h[l][i][i + v] = max(next_h[l][i][i + v], r - l);
                }
            }
        }
        for i in 0..h {
            for len in (2..(w + 1)).rev() {
                for l in 0..(w + 1 - len) {
                    let r = l + len;
                    let v = next_h[i][l][r];
                    next_h[i][l][r - 1] = max(next_h[i][l][r - 1], v);
                    next_h[i][l + 1][r] = max(next_h[i][l + 1][r], v);
                }
            }
        }
        for i in 0..w {
            for len in (2..(h + 1)).rev() {
                for l in 0..(h + 1 - len) {
                    let r = l + len;
                    let v = next_w[i][l][r];
                    next_w[i][l][r - 1] = max(next_w[i][l][r - 1], v);
                    next_w[i][l + 1][r] = max(next_w[i][l + 1][r], v);
                }
            }
        }
        dp_h = next_h;
        dp_w = next_w;
    }
    println!("{}", ans);
}

fn main() {
    run();
}