// 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];
                    assert!(ne <= yy);
                    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];
                    assert!(ne <= xx);
                    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); // ?
                    }
                }
            }
        }

        for i in 0..w {
            for y in 0..h+1 {
                let mut yy = dp_row[to][i][w-1][y];
                for j in (i..w).rev() {
                    yy = max(yy, dp_row[to][i][j][y]);
                    dp_row[to][i][j][y] = yy;
                }
            }
        }
        for i in 0..h {
            for x in 0..w+1 {
                let mut xx = dp_col[to][i][h-1][x];
                for j in (i..h).rev() {
                    xx = max(xx, dp_col[to][i][j][x]);
                    dp_col[to][i][j][x] = xx;
                }
            }
        }

        if dp_row[to][0][w-1][0] == h || dp_col[to][0][h-1][0] == w {
            println!("{}", comp);
            return;
        }
    }
    unreachable!("too large complexity");
}


Measured memory_bytes: 219388