function Main(input) {
  input = input.split("\n");
  var H = parseInt(input[0].split(" ")[0]);
  var W = parseInt(input[0].split(" ")[1]);
  input.shift();
  input.splice(H,input.length);
  var S = input.map(i => i.split(""));
  var edge = new Array();
  var id=0;
  for(var i=0;i<H;i++){
    for(var j=0;j<W;j++){
      if(S[i][j]=="."){
        if(i!=H-1){
          if(S[i+1][j]=="."){
            edge.push([id,id+W]);
          }
        }
        if(j!=W-1){
        if(S[i][j+1]=="."){
            edge.push([id,id+1]);
          }
        }
      }
      id++;
    }
  }
  var queue=new Array();
  var save = new Array(H*W).fill(0);
  var dist = new Array(H*W);
  var d =0;
  save[0]=1;
  queue.push([0,0]);
  var start=-1;
  while(queue.length!=0&&start==-1){
    var now =queue[0][0];
    var d =queue[0][1];
    dist[now]=[d,0];
    queue.shift();
    var flag=0;
    for(var i=0;i<edge.length;i++){
      if(edge[i][0]==now&&save[edge[i][1]]==0){
        queue.push([edge[i][1],d+1]);
        save[edge[i][1]]=1;
        flag=1;
      }
      if(edge[i][1]==now&&save[edge[i][0]]==0){
        queue.push([edge[i][0],d+1]);
        save[edge[i][0]]=1;
        flag=1;
      }
    }
    if(flag!=1){
      start=now;
    }
  }

  var queue=new Array();
  var save = new Array(H*W).fill(0);
  var dist = new Array(H*W);
  var d =0;
  save[start]=1;
  var ans =0;
  queue.push([start,0]);
  while(queue.length!=0){
    var now =queue[0][0];
    var d =queue[0][1];
    dist[now]=[d,0];
    ans = Math.max(ans,d);
    queue.shift();
    var flag=0;
    for(var i=0;i<edge.length;i++){
      if(edge[i][0]==now&&save[edge[i][1]]==0){
        queue.push([edge[i][1],d+1]);
        save[edge[i][1]]=1;
        flag=1;
      }
      if(edge[i][1]==now&&save[edge[i][0]]==0){
        queue.push([edge[i][0],d+1]);
        save[edge[i][0]]=1;
        flag=1;
      }
    }
    if(flag!=1){
      dist[now][1]=1;
    }
  }


  console.log('%s', ans);
  return ans;
}

function debug(n){
  var input = document.getElementById("input"+n).value;
  var ans=Main(input);
  var result="WA";
  if(ans==document.getElementById("output"+n).value.split("\n")[0]){
    result="AC";
  }
  document.getElementById("result"+n).innerHTML=result;
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));