#include <stdlib.h>
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
using ll=long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
inline constexpr ll gcd(ll a,ll b){if(!a||!b)return 0;while(b){ll c=b;b=a%b;a=c;}return a;}
inline constexpr ll lcm(ll a,ll b){if(!a||!b)return 0;return a*b/gcd(a,b);}

int main() {
  long long a,b;
  cin>>a>>b;
  vector<string> vec(a+2);
  string test;
  rep(i,b+2){
      test.append("#");
  }
  vec.at(0)=test;
  vec.at(a+1)=test;
  rep(i,a){
    string aaa;
    cin>>aaa;
    vec.at(i+1)="#";
    vec.at(i+1).append(aaa);
    vec.at(i+1).append("#");
  }
  rep(i,a+2){
      cout<<vec.at(i)<<endl;
  }
  int ans=1000000000;
  
    rep(i,a){
        rep(j,b){
            rep(k,a){
                rep(l,b){
                    int sx=i+2;
                    int sy=j+2;
                    int gx=k+2;
                    int gy=l+2;
                    if(sx!=gx&&sy!=gy){
                        vector<vector<int>> dist(a+2,vector<int>(b+2,-1));
                        dist.at(sy-1).at(sx-1)=0;
                        queue<pair<int,int>> que;
                        que.push(make_pair(sy-1,sx-1));
                        while(!que.empty()){
                            pair<int,int> currentnum=que.front();
                            int numy=currentnum.first;
                            int numx=currentnum.second;
                            //cout<<numy<<numx<<endl;
                            que.pop();
                            if(numy>=0&&numx>=0&&numy<a-1&&numx<b-1){
                            if(vec.at(numy+1).at(numx)!='#'){
                                if(dist.at(numy+1).at(numx)==-1){
                                dist.at(numy+1).at(numx)=dist.at(numy).at(numx)+1;
                                que.push(make_pair(numy+1,numx));
                                }
                            }
                            if(vec.at(numy-1).at(numx)!='#'){
                                if(dist.at(numy-1).at(numx)==-1){
                                dist.at(numy-1).at(numx)=dist.at(numy).at(numx)+1;
                                que.push(make_pair(numy-1,numx));
                                }
                            }
                            if(vec.at(numy).at(numx+1)!='#'){
                                if(dist.at(numy).at(numx+1)==-1){
                                dist.at(numy).at(numx+1)=dist.at(numy).at(numx)+1;
                                que.push(make_pair(numy,numx+1));
                                }
                            }
                            if(vec.at(numy).at(numx-1)!='#'){
                                if(dist.at(numy).at(numx-1)==-1){
                                dist.at(numy).at(numx-1)=dist.at(numy).at(numx)+1;
                                que.push(make_pair(numy,numx-1));
                                }
                            }
                            }
                        }
                        if(dist.at(gy-1).at(gx-1)<ans&&dist.at(gy-1).at(gx-1)!=-1){
                            ans=dist.at(gy-1).at(gx-1);
                        }
                    }
                }
            }
        }
    }
    cout<<ans<<endl;

}
