Language: C++

using namespace std;
int main(){
    int h,w,ans=0,l[4]={0,0,1,-1},m[4]={1,-1,0,0};
    cin >> h >> w ;
    vector<vector<char>> maze(h,vector<char> (w));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++) cin >> maze[i][j];
    }
    function<int(int,int,int,int)> cost = [&](int sx,int sy,int gx,int gy){
        vector<vector<int>> dist(h,vector<int> (w,-1));
        queue<pair<int,int>> que;
        dist[sx][sy] = 0;
        que.push(make_pair(sx,sy));
        while(!que.empty()){
            pair<int,int> d = que.front();
            int x,y;
            tie(x,y)=d;
            que.pop();
            for(int i=0;i<4;i++){
                if(x+l[i]<0||h<=x+l[i]) continue;
                if(y+m[i]<0||w<=y+m[i]) continue;
                if(maze[x+l[i]][y+m[i]]=='#') continue;
                if(dist[x+l[i]][y+m[i]]!=-1) continue;
                dist[x+l[i]][y+m[i]] = dist[x][y]+1;
                que.push(make_pair(x+l[i],y+m[i]));
            }
        }
        return dist[gx][gy];
    };
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            for(int k=0;k<h;k++){
                for(int p=0;p<w;p++) ans = max(ans,cost(i,j,k,p));
            }
        }
    }
    cout << ans << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:4:5: error: 'cin' was not declared in this scope
    4 |     cin >> h >> w ;
      |     ^~~
a.cc:1:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
  +++ |+#include <iostream>
    1 | using namespace std;
a.cc:5:5: error: 'vector' was not declared in this scope
    5 |     vector<vector<char>> maze(h,vector<char> (w));
      |     ^~~~~~
a.cc:1:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
  +++ |+#include <vector>
    1 | using namespace std;
a.cc:5:19: error: expected primary-expression before 'char'
    5 |     vector<vector<char>> maze(h,vector<char> (w));
      |                   ^~~~
a.cc:7:37: error: 'maze' was not declared in this scope
    7 |         for(int j=0;j<w;j++) cin >> maze[i][j];
      |                                     ^~~~
a.cc:9:5: error: 'function' was not declared in this scope
    9 |     function<int(int,int,int,int)> cost = [&](int sx,int sy,int gx,int gy){
      |     ^~~~~~~~
a.cc:1:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
  +++ |+#include <functional>
    1 | using namespace std;
a.cc:9:33: error: expression list treated as compound expression in functional cast [-fpermissive]
    9 |     function<int(int,int,int,int)> cost = [&](int sx,int sy,int gx,int gy){
      |                                 ^
a.cc:9:14: error: expected primary-expression before 'int'
    9 |     function<int(int,int,int,int)> cost = [&](int sx,int sy,int gx,int gy){
      |              ^~~
a.cc:33:52: error: 'cost' was not declared in this scope; did you mean 'const'?
   33 |                 for(int p=0;p<w;p++) ans = max(ans,cost(i,j,k,p));
      |                                                    ^~~~
      |                                                    const
a.cc:33:44: error: 'max' was not declared in this scope
   33 |                 for(int p=0;p<w;p++) ans = max(ans,cost(i,j,k,p));
      |                                            ^~~
a.cc:37:5: error: 'cout' was not declared in this scope
   37 |     cout << ans << endl;
      |     ^~~~
a.cc:37:5: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:37:20: error: 'endl' was not declared in this scope
   37 |     cout << ans << endl;
      |                    ^~~~
a.cc:1:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
  +++ |+#include <ostream>
    1 | using namespace std;