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


Measured memory_bytes: 221436