#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .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 ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
 
    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (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, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

use std::collections::VecDeque;

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

    let mut maximum = 0;

    for (i, mm) in maze.iter().enumerate() {
        for (j, m) in mm.iter().enumerate() {
            let mut dist = vec![vec![-1; w]; h];
            dist[i][j] = 0;
            if *m == '.' {
                let (_, length) = bfs((i, j), &maze, &mut dist);
                if length > maximum {
                    maximum = length;
                }
                //println!("length: {}", length);
            }
        }
    }

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

fn bfs(start: (usize, usize), maze: &Vec<Vec<char>>, dist: &mut Vec<Vec<isize>>) -> ((usize, usize), isize)
{
    //let prev_x = vec![vec![-1; width]; height];
    //let prev_y = vec![vec![-1; width]; height];

    let height = maze.len();
    let width = maze[0].len();
    let mut maximum = 0;
    let mut goal = (0, 0);

    let dx = vec![1, 0, -1, 0];
    let dy = vec![0, 1, 0, -1];

    let mut que = VecDeque::new();
    que.push_back(start);

    while !que.is_empty() {
        let current_pos = que.pop_front().unwrap();
        let x = current_pos.0;
        let y = current_pos.1;
    
        for d in 0..4 {
            let next_x = x as isize + dx[d];
            let next_y = y as isize + dy[d];
            if next_x < 0 || next_x >= height as isize || next_y < 0 || next_y >= width as isize {
                continue;
            }
            if maze[next_x as usize][next_y as usize] == '#' {
                continue;
            }
            if dist[next_x as usize][next_y as usize] == -1 {
                que.push_back((next_x as usize, next_y as usize));
                dist[next_x as usize][next_y as usize] = dist[x][y] + 1;
                if dist[next_x as usize][next_y as usize] > maximum {
                    maximum = dist[next_x as usize][next_y as usize];
                    goal = (next_x as usize, next_y as usize);
                    //println!("goal: ({}, {}) , length: {}", goal.0, goal.1, maximum);
                }
                //prev_x[next_x as usize][next_y as usize] = x as isize;
                //prev_y[next_x as usize][next_y as usize] = y as isize;
            }
        }
    }

    (goal, maximum)
}


Measured memory_bytes: 4352