Language: C++

#pragma region Include
#include <bits/stdc++.h>
// #include <algorithm>
// #include <bitset>
// #include <climits>
// #include <cmath>
// #include <iostream>
// #include <iterator>
// #include <map>
// #include <queue>
// #include <sstream>
// #include <stack>
// #include <string>
// #include <unordered_map>
// #include <unordered_set>
// #include <vector>
#pragma endregion

using namespace std;

#pragma region Macros
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define newl "\n"
#define out(x) cout << (x) << newl
#define dump(x) \
  cout << "Line " << __LINE__ << " : " << #x << " = " << (x) << newl
template <class T>
using VV = vector<vector<T>>;
#define Uni(v) (v).erase(unique((v).begin(), (v).end()), (v).end());

template <typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
  os << "[";
  rep(i, vec.size()) os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  os << "]";
  return os;
}

template <typename T>
ostream& operator<<(ostream& os, vector<vector<T>>& mat) {
  os << "[";
  rep(i, mat.size()) os << mat[i] << (i + 1 == mat.size() ? "" : ",\n ");
  os << "]";
  return os;
}

template <typename T>
istream& operator>>(istream& is, vector<T>& vec) {
  for (T& x : vec) is >> x;
  return is;
}

template <typename V, typename H>
void resize(vector<V>& vec, const H head) {
  vec.resize(head);
}

template <typename V, typename H, typename... T>
void resize(vector<V>& vec, const H& head, const T... tail) {
  vec.resize(head);
  for (auto& v : vec) resize(v, tail...);
}
constexpr double EPS = 1e-10;
const int INF = 0x3f3f3f3f;

string tostring(int i, int digit) {
  auto cur = cout.flags();
  stringstream ss;
  ss << setw(4) << setfill('0') << i;
  string ret = ss.str();
  cout.flags(cur);
  return ret;
}

template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

#pragma endregion

bool inbound(int x, int y, int N, int M) {
  return 0 <= x && x < N && 0 <= y && y < M;
}

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

  vvi seen;
  vector<vector<char>> board;
  queue<pii> bfs;

  int N, M;
  cin >> N >> M;
  seen.assign(N, vi(M, -1));
  board.assign(N, vector<char>(M, 0));
  pii start;
  bool start_set = false;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      cin >> board[i][j];
      if (board[i][j] == '.' && start_set == false) {
        start_set = true;
        start = mp(i, j);
        seen[i][j] = 0;
      }
    }
  }
  vvi dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

  bfs.push(start);
  while (!bfs.empty()) {
    pii next = bfs.front();
    bfs.pop();
    for (int k = 0; k < 4; ++k) {
      int x = next.fi + dir[k][0];
      int y = next.se + dir[k][1];
      if (!inbound(x, y, N, M)) {
        continue;
      }
      if (seen[x][y] >= 0) {
        continue;
      }
      if (board[x][y] == '.') {
        bfs.push(mp(x, y));
        seen[x][y] = seen[next.fi][next.se] + 1;
      }
    }
  }
  // out(seen);

  int maxdist = 0;
  vector<pair<int, int>> dest(0);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      if (seen[i][j] > maxdist && seen[i][j] > 0) {
        dest.assign(1, mp(i, j));
        maxdist = seen[i][j];
      } else if (seen[i][j] == maxdist && seen[i][j] > 0) {
        dest.pb(mp(i, j));
      }
    }
  }


  int maxdist = -1;
  rep(i, dest.size()) {
    seen.assign(N, vi(M, -1));
    start = dest[i];
    seen[start.fi][start.se] = 0;
    bfs.push(start);
    while (!bfs.empty()) {
      pii next = bfs.front();
      bfs.pop();
      for (int k = 0; k < 4; ++k) {
        int x = next.fi + dir[k][0];
        int y = next.se + dir[k][1];
        if (!inbound(x, y, N, M)) {
          continue;
        }
        if (seen[x][y] >= 0) {
          continue;
        }
        if (board[x][y] == '.') {
          bfs.push(mp(x, y));
          seen[x][y] = seen[next.fi][next.se] + 1;
        }
      }
    }
    // out(seen);

    for (int x = 0; x < N; ++x) {
      for (int y = 0; y < M; ++y) {
        if (seen[x][y] > maxdist) {
          maxdist = seen[x][y];
        }
      }
    }
  }
  out(maxdist);
  return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:172:7: error: redeclaration of 'int maxdist'
  172 |   int maxdist = -1;
      |       ^~~~~~~
a.cc:158:7: note: 'int maxdist' previously declared here
  158 |   int maxdist = 0;
      |       ^~~~~~~