Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
AuThOr GaRyMr
*/
#include <bits/stdc++.h>
#define rb(a, b, c) for (int a = b; a <= c; ++a)
#define rl(a, b, c) for (int a = b; a >= c; --a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a, b) make_pair(a, b)
#define FIR first
#define SEC second
#define FREO freopen("check.out", "w", stdout)
#define rep(a, b) for (int a = 0; a < b; ++a)
#define KEEP while (1)
#define SRAND                                                                  \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng() % a
#define ALL(a) a.begin(), a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> mp;
typedef pair<mp, mp> superpair;
int n, m;
int v[22][22];
char maze[22][22];
int res = 0;
bool check(int x, int y) {
  if (maze[x][y] == '.' && !v[x][y])
    return 1;
  return 0;
}
void bfs(int now) {
  rb(i, 1, n) rb(j, 1, m) v[i][j] = 0;
  queue<mp> q;
  q.push(II(now, 0));
  while (!q.empty()) {
    int now_ = q.front().FIR, step = q.front().SEC;
    q.pop();
    res = max(res, step);
    int x, y;
    x = now_ / 100, y = now_ % 100;
    v[x][y] = 1;
    if (x != 1) {
      if (check(x - 1, y)) {
        q.push(II((x - 1) * 100 + y, step + 1));
      }
    }
    if (x != n) {
      if (check(x + 1, y)) {
        q.push(II((x + 1) * 100 + y, step + 1));
      }
    }
    if (y != 1) {
      if (check(x, y - 1)) {
        q.push(II((x)*100 + y - 1, step + 1));
      }
    }
    if (y != m) {
      if (check(x, y + 1)) {
        q.push(II((x)*100 + y + 1, step + 1));
      }
    }
  }
}
int main() {
  cin >> n >> m;
  rb(i, 1, n) rb(j, 1, m) cin >> maze[i][j];
  rb(i, 1, n) rb(j, 1, m) {
    if (maze[i][j] == '.')
      bfs(i * 100 + j);
  }
  cout << res;
  return 0;
}


Fixed submission:
/*
AuThOr GaRyMr
*/
#include <bits/stdc++.h>
#define rb(a, b, c) for (int a = b; a <= c; ++a)
#define rl(a, b, c) for (int a = b; a >= c; --a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a, b) make_pair(a, b)
#define FIR first
#define SEC second
#define FREO freopen("check.out", "w", stdout)
#define rep(a, b) for (int a = 0; a < b; ++a)
#define KEEP while (1)
#define SRAND                                                                  \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng() % a
#define ALL(a) a.begin(), a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> mp;
typedef pair<mp, mp> superpair;
int n, m;
int v[22][22];
char maze[22][22];
int res = 0;
bool check(int x, int y) {
  if (maze[x][y] == '.' && !v[x][y])
    return 1;
  return 0;
}
void bfs(int now) {
  rb(i, 1, n) rb(j, 1, m) v[i][j] = 0;
  queue<mp> q;
  q.push(II(now, 0));
  while (!q.empty()) {
    int now_ = q.front().FIR, step = q.front().SEC;
    q.pop();
    res = max(res, step);
    int x, y;
    x = now_ / 100, y = now_ % 100;
    if (v[x][y])
      continue;
    v[x][y] = 1;
    if (x != 1) {
      if (check(x - 1, y)) {
        q.push(II((x - 1) * 100 + y, step + 1));
      }
    }
    if (x != n) {
      if (check(x + 1, y)) {
        q.push(II((x + 1) * 100 + y, step + 1));
      }
    }
    if (y != 1) {
      if (check(x, y - 1)) {
        q.push(II((x)*100 + y - 1, step + 1));
      }
    }
    if (y != m) {
      if (check(x, y + 1)) {
        q.push(II((x)*100 + y + 1, step + 1));
      }
    }
  }
}
int main() {
  cin >> n >> m;
  rb(i, 1, n) rb(j, 1, m) cin >> maze[i][j];
  rb(i, 1, n) rb(j, 1, m) {
    if (maze[i][j] == '.')
      bfs(i * 100 + j);
  }
  cout << res;
  return 0;
}