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::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

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

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



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

    let mut width = vec![vec![vec![vec![0; w+1]; h]; h]; 2];
    let mut height = vec![vec![vec![vec![0; h+1]; w]; w]; 2];
    for i in 0..h {
        for j in 0..h {
            for k in 0..w+1 {
                width[0][i][j][k] = k;
                width[1][i][j][k] = k;
            }
        }
    }
    for i in 0..w {
        for j in 0..w {
            for k in 0..h+1 {
                height[0][i][j][k] = k;
                height[1][i][j][k] = k;
            }
        }
    }

    for i in 0..h {
        let mut maxw = 0;
        let mut k = 0;
        while k < w {
            while maxw < w && table[i][k] == table[i][maxw] {
                maxw += 1;
            }
            while k < maxw {
                width[0][i][i][k] = maxw;
                k += 1;
            }
        }
    }
    for i in 0..w {
        let mut maxh = 0;
        let mut k = 0;
        while k < h {
            while maxh < h && table[k][i] == table[maxh][i] {
                maxh += 1;
            }
            while k < maxh {
                height[0][i][i][k] = maxh;
                k += 1;
            }
        }
    }


    for i in 0..h {
        for k in 0..w {
            let mut hasw = table[i][k] == '.';
            let mut hasb = table[i][k] == '#';
            let mut best_length = width[0][i][i][k];
            for j in i+1..h {
                hasw |= table[j][k] == '.';
                hasb |= table[j][k] == '#';
                best_length = min(best_length, width[0][j][j][k]);
                if hasw && hasb {
                    break;
                }
                width[0][i][j][k] = best_length;
            }
        }
    }

    for i in 0..w {
        for k in 0..h {
            let mut hasw = table[k][i] == '.';
            let mut hasb = table[k][i] == '#';
            let mut best_length = height[0][i][i][k];
            for j in i+1..w {
                hasw |= table[k][j] == '.';
                hasb |= table[k][j] == '#';
                best_length = min(best_length, height[0][j][j][k]);
                if hasw && hasb {
                    break;
                }
                height[0][i][j][k] = best_length;
            }
        }
    }

    let mut cur = 0;
    loop {
        let fr = cur % 2;
        let to = 1 - fr;
        if width[fr][0][h-1][0] == w || height[fr][0][w-1][0] == h {
            break;
        }

        for i in 0..h {
            for j in i..h {
                for k in 0..w {
                    width[to][i][j][k] = max(width[to][i][j][k], width[fr][i][j][width[fr][i][j][k]]);
                    if width[to][i][j][k] > k {
                        let ki = min(w-1, width[to][i][j][k]-1);
                        height[to][k][ki][i] = max(height[to][k][ki][i], j+1);
                    }
                }
            }
        }

        for i in 0..w {
            for j in i..w {
                for k in 0..h {
                    height[to][i][j][k] = max(height[to][i][j][k], height[fr][i][j][height[fr][i][j][k]]);
                    if  height[to][i][j][k] > k {
                        let ki = min(h-1, height[to][i][j][k]-1);
                        width[to][k][ki][i] = max(width[to][k][ki][i], j+1);
                    }
                }
            }
        }
        cur += 1;
    }


    println!("{}", cur);
}