#[allow(dead_code)]
fn is_whitespace(c: &char) -> bool {
    c.is_whitespace()
}

#[allow(dead_code)]
fn is_eol(c: &char) -> bool {
    ['\r', '\n'].contains(c)
}

#[allow(unused_macros)]
macro_rules! input {
    ($var:ident, $($r:tt)*) => {
        input_inner!{$var, $($r)*}
    };

    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        #[allow(unused_mut, unused_variables, bare_trait_objects)]
        let mut next = move |f: &Fn(&char)->bool| -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|f(&c))
                .take_while(|c|!f(&c))
                .collect()
        };
        input_inner!{next, $($r)*};
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

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

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; eol ]) => {
        $next(&is_eol).split(' ').map(|x| x.parse::<$t>().unwrap()).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, parser) => {
        $next
    };

    ($next:expr, $t:ty) => {
        $next(&is_whitespace).parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BinaryHeap, HashMap};

#[allow(dead_code)]
type Graph = Vec<Vec<usize>>;
#[allow(dead_code)]
const M: usize = 1e9 as usize + 9;

const dy: [i64; 4] = [0, 1, 0, -1];
const dx: [i64; 4] = [1, 0, -1, 0];

fn bfs(y: usize, x: usize, table: &Vec<Vec<bool>>) -> usize {
    let mut queue = std::collections::VecDeque::new();
    let mut memo = vec![vec![false; table[0].len()]; table.len()];
    queue.push_back((y, x, 0));
    let mut m = 0;
    while !queue.is_empty() {
        let top = queue.pop_front().unwrap();
        let y = top.0;
        let x = top.1;
        let c = top.2;
        if memo[y][x] {
            continue;
        }
        memo[y][x] = true;
        m = max(m, c);
        for i in 0..4 {
            let ny = y as i64 + dy[i];
            let nx = x as i64 + dx[i];
            if ny >= 0 && ny < table.len() as i64 && nx >= 0 && nx < table[0].len() as i64 {
                let ny = ny as usize;
                let nx = nx as usize;
                if !table[ny][nx] || memo[ny][nx] {
                    continue;
                }
                queue.push_back((ny, nx, c + 1));
            }
        }
    }
    return m;
}

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

    let mut table2 = vec![vec![false; w]; h];
    for i in 0..h {
        for j in 0..w {
            if table[i][j] == '.' {
                table2[i][j] = true;
            }
        }
    }

    let mut res = 0;
    for i in 0..h {
        for j in 0..w {
            if table2[i][j] {
                res = max(res, bfs(i, j, &table2));
            }
        }
    }
    println!("{}", res);
}


Measured memory_bytes: 4352