Language: C++

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <unordered_map>
#include <set>
#include <map>
#define rep(i,n) for (int i=0; i<n;++i)
#define ALL(a)  (a).begin(),(a).end()
typedef long long ll;
using namespace std;
int H, W;
char s[21][21];
int dp[21][21]; 
typedef pair<pair<int, int>, int> P; 
int dh[4] = {1, 0, -1, 0}, dw[4] = {0, 1, 0, -1};


// 計算量はO(HW)
P bfs(int sh, int sw) {
  queue<P> que;
  que.push(P(make_pair(sh, sw), 0));
  int max_cnt = 0;
  int max_h = sh, max_w = sw;
  while(!que.empty()) {
    P p = que.front(); que.pop();
    int h = p.first.first, w = p.first.second, cnt = p.second;
    rep(i, 4) {
      int nh = h +dh[i], nw = w+dw[i];
      if ((nh<0) || (nh>=H) || (nw<0) || (nw>=W)) continue;
      if (s[nh][nw] == '#') continue;
      if (dp[nh][nw] != -1) continue;
      int next_cnt = cnt+1; 
      que.push(P(make_pair(nh, nw), next_cnt));
      dp[nh][nw] = next_cnt;
      max_cnt = next_cnt;
      max_h = nh;
      max_w = nw;
    }
  }
  
  return P(make_pair(max_h, max_w), max_cnt);
}

void init(int sh, int sw) {
  rep(i, H) rep(j, W) dp[i][j]=-1;
  dp[sh][sw] = 0; 
}

int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> H >> W;
  int sh,sw;
  rep(i, H) {
    rep(j, W) {
      cin >> s[i][j];
      if (s[i][j]=='.') {
        sh = i;
        sw = j;
      } 
    }
  }
  int ans = -1;
  rep(i, H){
    rep(j, W) {
      init(i, j);
      P p = bfs(i, j);
      if (ans < p.second) ans = p.second;
    }
  }

  cout << ans << endl;
  return 0;
}#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <unordered_map>
#include <set>
#include <map>
#define rep(i,n) for (int i=0; i<n;++i)
#define ALL(a)  (a).begin(),(a).end()
typedef long long ll;
using namespace std;
int H, W;
char s[21][21];
int dp[21][21]; 
typedef pair<pair<int, int>, int> P; 
int dh[4] = {1, 0, -1, 0}, dw[4] = {0, 1, 0, -1};


// 計算量はO(HW)
P bfs(int sh, int sw) {
  queue<P> que;
  que.push(P(make_pair(sh, sw), 0));
  int max_cnt = 0;
  int max_h = sh, max_w = sw;
  while(!que.empty()) {
    P p = que.front(); que.pop();
    int h = p.first.first, w = p.first.second, cnt = p.second;
    rep(i, 4) {
      int nh = h +dh[i], nw = w+dw[i];
      if ((nh<0) || (nh>=H) || (nw<0) || (nw>=W)) continue;
      if (s[nh][nw] == '#') continue;
      if (dp[nh][nw] != -1) continue;
      int next_cnt = cnt+1; 
      que.push(P(make_pair(nh, nw), next_cnt));
      dp[nh][nw] = next_cnt;
      max_cnt = next_cnt;
      max_h = nh;
      max_w = nw;
    }
  }
  
  return P(make_pair(max_h, max_w), max_cnt);
}

void init(int sh, int sw) {
  rep(i, H) rep(j, W) dp[i][j]=-1;
  dp[sh][sw] = 0; 
}

int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> H >> W;
  int sh,sw;
  rep(i, H) {
    rep(j, W) {
      cin >> s[i][j];
      if (s[i][j]=='.') {
        sh = i;
        sw = j;
      } 
    }
  }
  int ans = -1;
  rep(i, H){
    rep(j, W) {
      init(i, j);
      P p = bfs(i, j);
      if (ans < p.second) ans = p.second;
    }
  }

  cout << ans << endl;
  return 0;
}

Compiler output:
a.cc:84:2: error: stray '#' in program
   84 | }#include <algorithm>
      |  ^
a.cc:84:3: error: 'include' does not name a type
   84 | }#include <algorithm>
      |   ^~~~~~~
a.cc:102:5: error: redefinition of 'int H'
  102 | int H, W;
      |     ^
a.cc:19:5: note: 'int H' previously declared here
   19 | int H, W;
      |     ^
a.cc:102:8: error: redefinition of 'int W'
  102 | int H, W;
      |        ^
a.cc:19:8: note: 'int W' previously declared here
   19 | int H, W;
      |        ^
a.cc:103:6: error: redefinition of 'char s [21][21]'
  103 | char s[21][21];
      |      ^
a.cc:20:6: note: 'char s [21][21]' previously declared here
   20 | char s[21][21];
      |      ^
a.cc:104:5: error: redefinition of 'int dp [21][21]'
  104 | int dp[21][21];
      |     ^~
a.cc:21:5: note: 'int dp [21][21]' previously declared here
   21 | int dp[21][21];
      |     ^~
a.cc:106:5: error: redefinition of 'int dh [4]'
  106 | int dh[4] = {1, 0, -1, 0}, dw[4] = {0, 1, 0, -1};
      |     ^~
a.cc:23:5: note: 'int dh [4]' previously defined here
   23 | int dh[4] = {1, 0, -1, 0}, dw[4] = {0, 1, 0, -1};
      |     ^~
a.cc:106:28: error: redefinition of 'int dw [4]'
  106 | int dh[4] = {1, 0, -1, 0}, dw[4] = {0, 1, 0, -1};
      |                            ^~
a.cc:23:28: note: 'int dw [4]' previously defined here
   23 | int dh[4] = {1, 0, -1, 0}, dw[4] = {0, 1, 0, -1};
      |                            ^~
a.cc:110:3: error: redefinition of 'P bfs(int, int)'
  110 | P bfs(int sh, int sw) {
      |   ^~~
a.cc:27:3: note: 'P bfs(int, int)' previously defined here
   27 | P bfs(int sh, int sw) {
      |   ^~~
a.cc:135:6: error: redefinition of 'void init(int, int)'
  135 | void init(int sh, int sw) {
      |      ^~~~
a.cc:52:6: note: 'void init(int, int)' previously defined here
   52 | void init(int sh, int sw) {
      |      ^~~~
a.cc:140:5: error: redefinition of 'int main()'
  140 | int main() {
      |     ^~~~
a.cc:57:5: note: 'int main()' previously defined here
   57 | int main() {
      |     ^~~~