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)

// https://atcoder.jp/contests/agc033/tasks/agc033_d
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
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)*}
    };
}

#[allow(unused_macros)]
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)*}
    };

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

#[allow(unused_macros)]
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, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($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")
    };
}

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

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

    let mut table = dvec!(0; h, w);
    for i in 0..h {
        for j in 0..w {
            table[i][j] = ifv!(a[i][j] == '.', 0, 1);
        }
    }
    let mut sumtable = dvec!(0i32; h+1, w+1);
    for i in 0..h {
        for j in 0..w {
            sumtable[i+1][j+1] = sumtable[i][j+1] + sumtable[i+1][j] - sumtable[i][j] + table[i][j];
        }
    }

    let range = |fy:usize,ty:usize,fx:usize,tx:usize| -> i32 {
        sumtable[ty][tx] - sumtable[ty][fx] - sumtable[fy][tx] + sumtable[fy][fx]
    };
    let is_mono = |fy:usize,ty:usize,fx:usize,tx:usize| {
        let num = range(fy,ty,fx,tx);
        let size = ((ty-fy)*(tx-fx)) as i32;
        num == 0 || num == size
    };

    let mut dp_col = dvec!(0; 2, h, h, w+1);
    let mut dp_row = dvec!(0; 2, w, w, h+1);

    for i in 0..h {
        for j in i..h {
            let mut tx = 0;
            for x in 0..w+1 {
                tx = max(tx, x);
                while tx <= w && is_mono(i, j+1, x, tx) {
                    tx += 1;
                }
                dp_col[0][i][j][x] = tx-1;
            }
        }
    }
    for i in 0..w {
        for j in i..w {
            let mut ty = 0;
            for y in 0..h+1 {
                ty = max(ty, y);
                while ty <= h && is_mono(y, ty, i, j+1) {
                    ty += 1;
                }
                dp_row[0][i][j][y] = ty-1;
            }
        }
    }
    if dp_row[0][0][w-1][0] == h {
        println!("0");
        return;
    }

    // for i in 0..h {
    //     for j in i..h {
    //         debug!(i, j, dp_col[0][i][j]);
    //     }
    // }

    for comp in 1..(h+w+1) {
        let to = comp % 2;
        let fr = to ^ 1;
        for i in 0..w {
            for j in i..w {
                for y in 0..h+1 {
                    let ne = dp_row[fr][i][j][y];
                    let yy = dp_row[fr][i][j][ne];
                    dp_row[to][i][j][y] = max(dp_row[to][i][j][y], yy);
                    if y < h && y < yy {
                        dp_col[to][y][yy-1][i] = max(dp_col[to][y][yy-1][i], j+1);
                    }
                }
            }
        }
        for i in 0..h {
            for j in i..h {
                for x in 0..w+1 {
                    let ne = dp_col[fr][i][j][x];
                    let xx = dp_col[fr][i][j][ne];
                    dp_col[to][i][j][x] = max(dp_col[to][i][j][x], xx);
                    if x < w && x < xx {
                        dp_row[to][x][xx-1][i] = max(dp_row[to][x][xx-1][i], j+1);
                    }
                }
            }
        }
        if dp_row[to][0][w-1][0] == h || dp_col[to][0][h-1][0] == w {
            println!("{}", comp);
            return;
        }
    }
    unreachable!("too large complexity");
}