prithivMLmods/Coder-Stat huggingface dataset:
Problem p02803 (Python)

Buggy submission (status: Runtime Error):
import os
import sys

import numpy as np
from scipy.sparse.csgraph._shortest_path import floyd_warshall

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10**9)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
# MOD = 998244353

H, W = list(map(int, sys.stdin.buffer.readline().split()))
S = [sys.stdin.buffer.readline().decode().rstrip() for _ in range(H)]


def to_i(h, w):
    return h * W + w


N = H * W
graph = [[0] * N for _ in range(N)]
for h in range(H):
    for w in range(W):
        if S[h][w] == ".":
            if 0 <= h - 1 < H and S[h - 1][w] == ".":
                graph[to_i(h - 1, w)][to_i(h, w)] = 1
                graph[to_i(h, w)][to_i(h - 1, w)] = 1
            if 0 <= h + 1 < H and S[h + 1][w] == ".":
                graph[to_i(h + 1, w)][to_i(h, w)] = 1
                graph[to_i(h, w)][to_i(h + 1, w)] = 1
            if 0 <= w - 1 < W and S[h][w - 1] == ".":
                graph[to_i(h, w - 1)][to_i(h, w)] = 1
                graph[to_i(h, w)][to_i(h, w - 1)] = 1
            if 0 <= w + 1 < H and S[h][w + 1] == ".":
                graph[to_i(h, w + 1)][to_i(h, w)] = 1
                graph[to_i(h, w)][to_i(h, w + 1)] = 1
dist = floyd_warshall(graph, directed=False)
dist[dist == np.inf] = 0
print(int(dist.max()))


Fixed submission:
import os
import sys

import numpy as np
from scipy.sparse.csgraph._shortest_path import floyd_warshall

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10**9)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
# MOD = 998244353

H, W = list(map(int, sys.stdin.buffer.readline().split()))
S = [sys.stdin.buffer.readline().decode().rstrip() for _ in range(H)]


def to_i(h, w):
    return h * W + w


N = H * W
graph = [[0] * N for _ in range(N)]
for h in range(H):
    for w in range(W):
        if S[h][w] == ".":
            if 0 <= h - 1 < H and S[h - 1][w] == ".":
                graph[to_i(h - 1, w)][to_i(h, w)] = 1
                graph[to_i(h, w)][to_i(h - 1, w)] = 1
            if 0 <= h + 1 < H and S[h + 1][w] == ".":
                graph[to_i(h + 1, w)][to_i(h, w)] = 1
                graph[to_i(h, w)][to_i(h + 1, w)] = 1
            if 0 <= w - 1 < W and S[h][w - 1] == ".":
                graph[to_i(h, w - 1)][to_i(h, w)] = 1
                graph[to_i(h, w)][to_i(h, w - 1)] = 1
            if 0 <= w + 1 < W and S[h][w + 1] == ".":
                graph[to_i(h, w + 1)][to_i(h, w)] = 1
                graph[to_i(h, w)][to_i(h, w + 1)] = 1
dist = floyd_warshall(graph, directed=False)
dist[dist == np.inf] = 0
print(int(dist.max()))
Problem p02803 (Python)

Buggy submission (status: Runtime Error):
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall

H, W = map(int, input().split())
S = [input() for _ in range(H)]

g = [[0] * (H * W) for _ in range(H * W)]
for y in range(H):
    for x in range(W):
        if S[y][x] == "#":
            continue
        if y - 1 >= 0 and S[y - 1][x] != "#":
            g[y * W + x][(y - 1) * W + x] = 1
        if y + 1 < H and S[y + 1][x] != "#":
            g[y * W + x][(y + 1) * W + x] = 1
        if x - 1 >= 0 and S[y][x - 1] != "#":
            g[y * W + x][y * W + x - 1] = 1
        if x + 1 < W and S[y][x + 1] != "#":
            g[y * W + x][y * W + x + 1] = 1
g = floyd_warshall(csgraph_from_dense(g))

result = 0
for i in range(H * W):
    for j in range(H * W):
        if g[i][j] == 0:
            continue
        result = max(result, g[i][j])
print(int(result))


Fixed submission:
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall

H, W = map(int, input().split())
S = [input() for _ in range(H)]

g = [[0] * (H * W) for _ in range(H * W)]
for y in range(H):
    for x in range(W):
        if S[y][x] == "#":
            continue
        if y - 1 >= 0 and S[y - 1][x] != "#":
            g[y * W + x][(y - 1) * W + x] = 1
        if y + 1 < H and S[y + 1][x] != "#":
            g[y * W + x][(y + 1) * W + x] = 1
        if x - 1 >= 0 and S[y][x - 1] != "#":
            g[y * W + x][y * W + x - 1] = 1
        if x + 1 < W and S[y][x + 1] != "#":
            g[y * W + x][y * W + x + 1] = 1
g = floyd_warshall(csgraph_from_dense(g))

result = 0
for i in range(H * W):
    for j in range(H * W):
        if g[i][j] == float("inf"):
            continue
        result = max(result, g[i][j])
print(int(result))
Problem p02803 (Python)

Buggy submission (status: Runtime Error):
H, W = map(int, input().split())
S = [input() for _ in range(H)]


def f(i, j):
    t = [[-1] * W for _ in range(W)]
    t[i][j] = 0
    q = [(i, j)]
    while q:
        y, x = q.pop(0)
        if y - 1 >= 0 and S[y - 1][x] != "#" and t[y - 1][x] == -1:
            t[y - 1][x] = t[y][x] + 1
            q.append((y - 1, x))
        if y + 1 < H and S[y + 1][x] != "#" and t[y + 1][x] == -1:
            t[y + 1][x] = t[y][x] + 1
            q.append((y + 1, x))
        if x - 1 >= 0 and S[y][x - 1] != "#" and t[y][x - 1] == -1:
            t[y][x - 1] = t[y][x] + 1
            q.append((y, x - 1))
        if x + 1 < W and S[y][x + 1] != "#" and t[y][x + 1] == -1:
            t[y][x + 1] = t[y][x] + 1
            q.append((y, x + 1))
    return max(max(tt) for tt in t)


result = 0
for i in range(H):
    for j in range(W):
        if S[i][j] != "#":
            result = max(result, f(i, j))
print(result)


Fixed submission:
H, W = map(int, input().split())
S = [input() for _ in range(H)]


def f(i, j):
    t = [[-1] * W for _ in range(H)]
    t[i][j] = 0
    q = [(i, j)]
    while q:
        y, x = q.pop(0)
        if y - 1 >= 0 and S[y - 1][x] != "#" and t[y - 1][x] == -1:
            t[y - 1][x] = t[y][x] + 1
            q.append((y - 1, x))
        if y + 1 < H and S[y + 1][x] != "#" and t[y + 1][x] == -1:
            t[y + 1][x] = t[y][x] + 1
            q.append((y + 1, x))
        if x - 1 >= 0 and S[y][x - 1] != "#" and t[y][x - 1] == -1:
            t[y][x - 1] = t[y][x] + 1
            q.append((y, x - 1))
        if x + 1 < W and S[y][x + 1] != "#" and t[y][x + 1] == -1:
            t[y][x + 1] = t[y][x] + 1
            q.append((y, x + 1))
    return max(max(tt) for tt in t)


result = 0
for i in range(H):
    for j in range(W):
        if S[i][j] != "#":
            result = max(result, f(i, j))
print(result)
Problem p02803 (Python)

Buggy submission (status: Time Limit Exceeded):
from itertools import product


def solve(string):
    h, w, *s = string.split()
    h, w = map(int, [h, w])
    s = ["#" + _s + "#" for _s in s]
    s = ["#" * (w + 2)] + s + ["#" * (w + 2)]
    ans = 0
    d = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    for _h, _w in product(range(1, h + 1), range(1, w + 1)):
        if s[_h][_w] == "#":
            continue
        c = [[h * w] * (w + 2) for _ in range(h + 2)]
        c[_h][_w] = 0
        q = list()
        for x, y in d:
            if s[_h + x][_w + y] == ".":
                q.append((_h + x, _w + y, _h, _w))
        while q:
            cx, cy, px, py = q.pop()
            if c[px][py] + 1 >= c[cx][cy]:
                continue
            c[cx][cy] = c[px][py] + 1
            for x, y in d:
                nx = cx + x
                ny = cy + y
                if s[nx][ny] == ".":
                    q.append((nx, ny, cx, cy))
        for i, j in product(range(1, h + 1), range(1, w + 1)):
            ans = max(ans, c[i][j] * (s[i][j] == "."))
    return str(ans)


if __name__ == "__main__":
    import sys

    print(solve(sys.stdin.read().strip()))


Fixed submission:
from itertools import product


def solve(string):
    h, w, *s = string.split()
    h, w = map(int, [h, w])
    s = ["#" + _s + "#" for _s in s]
    s = ["#" * (w + 2)] + s + ["#" * (w + 2)]
    ans = 0
    d = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    for _h, _w in product(range(1, h + 1), range(1, w + 1)):
        if s[_h][_w] == "#":
            continue
        c = [[h * w] * (w + 2) for _ in range(h + 2)]
        c[_h][_w] = 0
        q = list()
        for x, y in d:
            if s[_h + x][_w + y] == ".":
                q.append((_h + x, _w + y, _h, _w))
        if len(q) > 2:
            continue
        while q:
            cx, cy, px, py = q.pop()
            if c[px][py] + 1 >= c[cx][cy]:
                continue
            c[cx][cy] = c[px][py] + 1
            for x, y in d:
                nx = cx + x
                ny = cy + y
                if s[nx][ny] == ".":
                    q.append((nx, ny, cx, cy))
        for i, j in product(range(1, h + 1), range(1, w + 1)):
            ans = max(ans, c[i][j] * (s[i][j] == "."))
    return str(ans)


if __name__ == "__main__":
    import sys

    print(solve(sys.stdin.read().strip()))
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 10000;
int d[20][20];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
char maze[20][20];
int h, w, hw;
int l = 0;
bool flg = true;
int bfs(int sx, int sy) {
  int hwc = 0;
  queue<P> que;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      d[i][j] = INF;
  que.push(P(sx, sy));
  d[sx][sy] = 0;
  while (hw > hwc) {
    P p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] != '#' &&
          d[nx][ny] == INF) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
        if (l < d[nx][ny]) {
          l = d[nx][ny];
        }
      }
    }
    hwc++;
  }
  return l;
}
int main() {
  string s;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    cin >> s;
    for (int j = 0; j < w; j++) {
      maze[i][j] = s[j];
    }
  }
  hw = h * w;
  int ans = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (maze[i][j] == '.')
        ans = max(ans, bfs(i, j));
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 10000;
int d[20][20];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
char maze[20][20];
int h, w, hw;
int l = 0;
bool flg = true;
int bfs(int sx, int sy) {
  int hwc = 0;
  queue<P> que;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      d[i][j] = INF;
  que.push(P(sx, sy));
  d[sx][sy] = 0;
  while (hw > hwc && que.size() > 0) {
    P p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] != '#' &&
          d[nx][ny] == INF) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
        if (l < d[nx][ny]) {
          l = d[nx][ny];
        }
      }
    }
    hwc++;
  }
  return l;
}
int main() {
  string s;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    cin >> s;
    for (int j = 0; j < w; j++) {
      maze[i][j] = s[j];
    }
  }
  hw = h * w;
  int ans = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (maze[i][j] == '.')
        ans = max(ans, bfs(i, j));
  cout << ans;
  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

int x[] = {0, 1, 0, -1};
int y[] = {1, 0, -1, 0};
bool M[21][21];
int H, W;
bool valid(int r, int c) {
  if (r < H && r >= 0 && c < W && c >= 0)
    return true;
  else
    return false;
}
bool v[21][21];
int BFS(int r, int c) {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      v[i][j] = 0;
    }
  }
  queue<pair<int, int>> q;
  q.push({r, c});
  v[r][c] = 1;
  q.push({-1, -1});
  int cnt = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    if (p.first == -1) {
      cnt++;
      if (!q.empty()) {
        q.push({-1, -1});
        continue;
      } else {
        break;
      }
    }
    r = p.first;
    c = p.second;

    for (int i = 0; i < 4; i++) {
      int R = r + x[i];
      int C = c + y[i];
      if (valid(R, C) && M[R][C] && !v[R][C]) {
        q.push({R, C});
        v[r][c] = 1;
      }
    }
  }
  return cnt - 1;
}

int main() {

  cin >> H >> W;
  char c;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c;
      if (c == '.')
        M[i][j] = 1;
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (M[i][j]) {
        int x = BFS(i, j);
        ans = max(ans, x);
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

int x[] = {0, 1, 0, -1};
int y[] = {1, 0, -1, 0};
bool M[21][21];
int H, W;
bool valid(int r, int c) {
  if (r < H && r >= 0 && c < W && c >= 0)
    return true;
  else
    return false;
}
bool v[21][21];
int BFS(int r, int c) {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      v[i][j] = 0;
    }
  }
  queue<pair<int, int>> q;
  q.push({r, c});
  v[r][c] = 1;
  q.push({-1, -1});
  int cnt = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    if (p.first == -1) {
      cnt++;
      if (!q.empty()) {
        q.push({-1, -1});
        continue;
      } else {
        break;
      }
    }
    r = p.first;
    c = p.second;

    for (int i = 0; i < 4; i++) {
      int R = r + x[i];
      int C = c + y[i];
      if (valid(R, C) && M[R][C] && !v[R][C]) {
        q.push({R, C});
        v[R][C] = 1;
      }
    }
  }
  return cnt - 1;
}

int main() {

  cin >> H >> W;
  char c;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c;
      if (c == '.')
        M[i][j] = 1;
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (M[i][j]) {
        int x = BFS(i, j);
        ans = max(ans, x);
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int H, W;
  char c[21][21];
  int check[21][21];
  int i, j;
  queue<pair<int, int>> que;
  cin >> H >> W;
  int ans = 0;
  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      cin >> c[i][j];
  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++) {
      if (c[i][j] == '#')
        continue;
      else {
        for (int k = 0; k < H; k++)
          for (int l = 0; l < W; l++)
            check[k][l] = -1e9;
        check[i][j] = 0;
        que.push(pair<int, int>(i, j));
        while (!que.empty()) {
          int x = que.front().first;
          int y = que.front().second;
          int dx[4] = {1, -1, 0, 0};
          int dy[4] = {0, 0, 1, -1};
          que.pop();
          for (int k = 0; k < 4; k++) {
            if (c[x + dx[k]][y + dy[k]] == '.' &&
                check[x + dx[k]][y + dy[k]] < check[x][y] + 1) {
              check[x + dx[k]][y + dy[k]] = check[x][y] + 1;
              que.push(pair<int, int>(x + dx[k], y + dy[k]));
            }
          }
        }
        for (int k = 0; k < H; k++)
          for (int l = 0; l < W; l++)
            if (check[k][l] > ans)
              ans = check[k][l];
      }
    }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int H, W;
  char c[21][21];
  int check[21][21];
  int i, j;
  queue<pair<int, int>> que;
  cin >> H >> W;
  int ans = 0;
  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      cin >> c[i][j];
  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++) {
      if (c[i][j] == '#')
        continue;
      else {
        for (int k = 0; k < H; k++)
          for (int l = 0; l < W; l++)
            check[k][l] = -1e9;
        check[i][j] = 0;
        que.push(pair<int, int>(i, j));
        while (!que.empty()) {
          int x = que.front().first;
          int y = que.front().second;
          int dx[4] = {1, -1, 0, 0};
          int dy[4] = {0, 0, 1, -1};
          que.pop();
          for (int k = 0; k < 4; k++) {
            if (c[x + dx[k]][y + dy[k]] == '.' &&
                check[x + dx[k]][y + dy[k]] == -1e9) {
              check[x + dx[k]][y + dy[k]] = check[x][y] + 1;
              que.push(pair<int, int>(x + dx[k], y + dy[k]));
            }
          }
        }
        for (int k = 0; k < H; k++)
          for (int l = 0; l < W; l++)
            if (check[k][l] > ans)
              ans = check[k][l];
      }
    }
  cout << ans << endl;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ln(a) cout << (a) << endl
#define debug(a, n)                                                            \
  rep(hoge, n) printf("%lld%c", a[hoge], hoge == (n)-1 ? '\n' : ' ');
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 1e9
#define LINF 1e18

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;

signed main() {
  int h, w;
  cin >> h >> w;
  string *maze = new string[h];
  rep(i, h) { cin >> maze[i]; }

  int ans = 0;
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  rep(i, h) {
    rep(j, w) {
      rep(k, h) {
        rep(l, w) {
          if (maze[i][j] == '#' || maze[k][l] == '#')
            continue;
          bool **used = new bool *[h];
          rep(m, h) {
            used[m] = new bool[w];
            fill(used[m], used[m] + w, false);
          }
          used[i][j] = true;
          queue<pair<P, int>> que;
          que.push(make_pair(P(i, j), 0));
          while (!que.empty()) {
            P p = que.front().first;
            int d = que.front().second;
            que.pop();
            if (p.first == k && p.second == l) {
              ans = max(ans, d);
              break;
            }
            rep(m, 4) {
              int nx = p.first + dx[m];
              int ny = p.second + dy[m];
              if (0 <= nx && nx < h && 0 <= ny && ny < w &&
                  maze[nx][ny] == '.') {
                que.push(make_pair(P(nx, ny), d + 1));
              }
            }
          }
        }
      }
    }
  }

  ln(ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ln(a) cout << (a) << endl
#define debug(a, n)                                                            \
  rep(hoge, n) printf("%lld%c", a[hoge], hoge == (n)-1 ? '\n' : ' ');
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 1e9
#define LINF 1e18

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;

signed main() {
  int h, w;
  cin >> h >> w;
  string *maze = new string[h];
  rep(i, h) { cin >> maze[i]; }

  int ans = 0;
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  rep(i, h) {
    rep(j, w) {
      if (maze[i][j] == '#')
        continue;
      bool **used = new bool *[h];
      rep(m, h) {
        used[m] = new bool[w];
        fill(used[m], used[m] + w, false);
      }
      used[i][j] = true;
      queue<pair<P, int>> que;
      que.push(make_pair(P(i, j), 0));
      while (!que.empty()) {
        P p = que.front().first;
        int d = que.front().second;
        que.pop();
        rep(m, 4) {
          int nx = p.first + dx[m];
          int ny = p.second + dy[m];
          if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == '.' &&
              !used[nx][ny]) {
            used[nx][ny] = true;
            que.push(make_pair(P(nx, ny), d + 1));
            ans = max(ans, d + 1);
          }
        }
      }
    }
  }

  ln(ans);
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
const int INF = 0x3fffffff;
int h, w;
vector<string> s;
vector<vector<int>> bfs(int x, int y) {
  vector<vector<int>> cost(h, vector<int>(h, INF));
  cost[x][y] = 0;
  queue<pair<int, int>> q;
  q.emplace(x, y);
  while (q.size()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int x2 = x + dx[i], y2 = y + dy[i];
      if (x2 < 0 || x2 >= h || y2 < 0 || y2 >= w || s[x2][y2] == '#')
        continue;
      if (cost[x2][y2] > cost[x][y] + 1) {
        cost[x2][y2] = cost[x][y] + 1;
        q.emplace(x2, y2);
      }
    }
  }
  return cost;
}
int main() {
  cin >> h >> w;
  s.resize(h);
  for (auto &&i : s)
    cin >> i;
  int ans = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (s[i][j] != '#') {
        auto cost = bfs(i, j);
        for (int i = 0; i < h; i++)
          for (int j = 0; j < w; j++)
            if (s[i][j] != '#' && ans < cost[i][j])
              ans = cost[i][j];
      }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
const int INF = 0x3fffffff;
int h, w;
vector<string> s;
vector<vector<int>> bfs(int x, int y) {
  vector<vector<int>> cost(h, vector<int>(w, INF));
  cost[x][y] = 0;
  queue<pair<int, int>> q;
  q.emplace(x, y);
  while (q.size()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int x2 = x + dx[i], y2 = y + dy[i];
      if (x2 < 0 || x2 >= h || y2 < 0 || y2 >= w || s[x2][y2] == '#')
        continue;
      if (cost[x2][y2] > cost[x][y] + 1) {
        cost[x2][y2] = cost[x][y] + 1;
        q.emplace(x2, y2);
      }
    }
  }
  return cost;
}
int main() {
  cin >> h >> w;
  s.resize(h);
  for (auto &&i : s)
    cin >> i;
  int ans = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (s[i][j] != '#') {
        auto cost = bfs(i, j);
        for (int i = 0; i < h; i++)
          for (int j = 0; j < w; j++)
            if (s[i][j] != '#' && ans < cost[i][j])
              ans = cost[i][j];
      }
  cout << ans << endl;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll = long long;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD 1000000007
const ll INF = 1e18;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int dy[4] = {-1, 0, 1, 0}, dx[4] = {0, 1, 0, -1};
int h, w;
vector<string> s(h);

int bfs(int sy, int sx) {
  queue<pii> q;
  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[sy][sx] = 0;
  q.push(pii(sy, sx));
  while (!q.empty()) {
    pii now = q.front();
    q.pop();
    int y = now.fi, x = now.se;
    rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (dist[ny][nx] == -1) {
        q.push(pii(ny, nx));
        dist[ny][nx] = dist[y][x] + 1;
      }
    }
  }
  int ret = 0;
  rep(i, h) rep(j, w) ret = max(ret, dist[i][j]);
  return ret;
}

int main(int argc, char const *argv[]) {
  int ans = 0;
  cin >> h >> w;
  rep(i, h) cin >> s[i];
  rep(i, h) rep(j, w) {
    if (s[i][j] == '#')
      continue;
    int res = bfs(i, j);
    ans = max(ans, res);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll = long long;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD 1000000007
const ll INF = 1e18;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int dy[4] = {-1, 0, 1, 0}, dx[4] = {0, 1, 0, -1};
int h, w;
string s[20];

int bfs(int sy, int sx) {
  queue<pii> q;
  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[sy][sx] = 0;
  q.push(pii(sy, sx));
  while (!q.empty()) {
    pii now = q.front();
    q.pop();
    int y = now.fi, x = now.se;
    rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (dist[ny][nx] == -1) {
        q.push(pii(ny, nx));
        dist[ny][nx] = dist[y][x] + 1;
      }
    }
  }
  int ret = 0;
  rep(i, h) rep(j, w) ret = max(ret, dist[i][j]);
  return ret;
}

int main(int argc, char const *argv[]) {
  int ans = 0;
  cin >> h >> w;
  rep(i, h) cin >> s[i];
  rep(i, h) rep(j, w) {
    if (s[i][j] == '#')
      continue;
    int res = bfs(i, j);
    ans = max(ans, res);
  }
  cout << ans << endl;
  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;

typedef pair<int, int> P;
int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};

const int INF = 1001001001;

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> g(h);
  rep(i, h) cin >> g[i];

  // init graph
  vector<vi> dist(h * w, vi(h * w, INF));
  rep(i, h * w) dist[i][i] = 0;

  rep(i, h) rep(j, w) {
    if (g[i][j] == '#')
      continue;
    rep(k, 4) {
      int dy = i + di[k];
      int dx = j + dj[k];
      if (dy < 0 or dy >= h or dx < 0 or dx >= w)
        continue;
      if (g[dy][dx] == '#')
        continue;
      dist[(i * h) + j][(dy * h) + dx] = 1;
    }
  }

  rep(k, h * w) {
    rep(i, h * w) {
      rep(j, h * w) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); }
    }
  }

  int ans = 0;
  rep(i, h * w) {
    rep(j, h * w) {
      if (dist[i][j] != INF)
        ans = max(ans, dist[i][j]);
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;

typedef pair<int, int> P;
int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};

const int INF = 1001001001;

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> g(h);
  rep(i, h) cin >> g[i];

  // init graph
  vector<vi> dist(h * w, vi(h * w, INF));
  rep(i, h * w) dist[i][i] = 0;

  rep(i, h) rep(j, w) {
    if (g[i][j] == '#')
      continue;
    rep(k, 4) {
      int dy = i + di[k];
      int dx = j + dj[k];
      if (dy < 0 or dy >= h or dx < 0 or dx >= w)
        continue;
      if (g[dy][dx] == '#')
        continue;
      dist[(i * w) + j][(dy * w) + dx] = 1;
    }
  }

  rep(k, h * w) {
    rep(i, h * w) {
      rep(j, h * w) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); }
    }
  }

  int ans = 0;
  rep(i, h * w) {
    rep(j, h * w) {
      if (dist[i][j] != INF)
        ans = max(ans, dist[i][j]);
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
// “Make it work, make it right, make it fast.” – Kent Beck
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dd;
const int siz = 1e6 + 5;
const int MOD = 1e9 + 7;
#define endl '\n'
#define deb(x) cout << #x << " = " << x << endl;
const int N = 20;
const int inf = 1e9 + 5;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int n, m;
string b[N];
int bfs(int i, int j) {
  queue<pair<int, int>> q;
  q.push(make_pair(i, j));
  int dist[N][N];
  memset(dist, inf, sizeof dist);
  dist[i][j] = 0;
  int maxi = 0;
  while (!q.empty()) {
    auto t = q.front();
    q.pop();
    maxi = max(maxi, dist[t.first][t.second]);
    for (int ii = 0; ii < 4; ii++) {
      int x = t.first + dx[ii], y = t.second + dy[ii];
      if (x < 0 || x >= n || y < 0 || y >= m || b[x][y] == '#')
        continue;
      if (dist[x][y] <= dist[t.first][t.second])
        continue;
      dist[x][y] = dist[t.first][t.second] + 1;
      q.emplace(x, y);
    }
  }

  return maxi;
}

void solve() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    b[i].clear();
    cin >> b[i];
  }

  int maxi = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (b[i][j] == '.')
        maxi = max(maxi, bfs(i, j));
    }
  }
  cout << maxi << endl;
}

int main() {
#ifndef RAY
  // freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
// “Make it work, make it right, make it fast.” – Kent Beck
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dd;
const int siz = 1e6 + 5;
const int MOD = 1e9 + 7;
#define endl '\n'
#define deb(x) cout << #x << " = " << x << endl;
const int N = 20;
const int inf = 1e9 + 5;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int n, m;
string b[N];
int bfs(int i, int j) {
  queue<pair<int, int>> q;
  q.push(make_pair(i, j));
  int dist[N][N];
  memset(dist, inf, sizeof dist);
  dist[i][j] = 0;
  int maxi = 0;
  while (!q.empty()) {
    auto t = q.front();
    q.pop();
    maxi = max(maxi, dist[t.first][t.second]);
    for (int ii = 0; ii < 4; ii++) {
      int x = t.first + dx[ii], y = t.second + dy[ii];
      if (x < 0 || x >= n || y < 0 || y >= m || b[x][y] == '#')
        continue;
      if (dist[x][y] <= dist[t.first][t.second] + 1)
        continue;
      dist[x][y] = dist[t.first][t.second] + 1;
      q.emplace(x, y);
    }
  }

  return maxi;
}

void solve() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    b[i].clear();
    cin >> b[i];
  }

  int maxi = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (b[i][j] == '.')
        maxi = max(maxi, bfs(i, j));
    }
  }
  cout << maxi << endl;
}

int main() {
#ifndef RAY
  // freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int INF = 1000000000;
  int h, w;
  cin >> h >> w;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  int ans = 0;
  rep(sy, h) rep(sx, w) {
    if (g[sy][sx] == '#')
      continue;
    vvi dp(h, vi(w, INF));
    queue<pair<int, int>> q;
    q.push({sy, sx});
    vi v = {1, 0, -1, 0, 1};
    dp[sy][sx] = 0;
    while (!q.empty()) {
      int y = q.front().first;
      int x = q.front().second;
      q.pop();
      rep(i, 4) {
        int ny = y + v[i];
        int nx = x + v[i + 1];
        if (ny >= h || nx >= w || ny < 0 || nx < 0)
          continue;
        if (g[ny][nx] == '#')
          continue;
        if (dp[ny][nx] > dp[y][x]) {
          dp[ny][nx] = dp[y][x] + 1;
          q.push({ny, nx});
        }
      }
    }
    rep(i, h) rep(j, w) {
      if (dp[i][j] == INF)
        continue;
      ans = max(ans, dp[i][j]);
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int INF = 1000000000;
  int h, w;
  cin >> h >> w;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  int ans = 0;
  rep(sy, h) rep(sx, w) {
    if (g[sy][sx] == '#')
      continue;
    vvi dp(h, vi(w, INF));
    queue<pair<int, int>> q;
    q.push({sy, sx});
    vi v = {1, 0, -1, 0, 1};
    dp[sy][sx] = 0;
    while (!q.empty()) {
      int y = q.front().first;
      int x = q.front().second;
      q.pop();
      rep(i, 4) {
        int ny = y + v[i];
        int nx = x + v[i + 1];
        if (ny >= h || nx >= w || ny < 0 || nx < 0)
          continue;
        if (g[ny][nx] == '#')
          continue;
        if (dp[ny][nx] > dp[y][x] + 1) {
          dp[ny][nx] = dp[y][x] + 1;
          q.push({ny, nx});
        }
      }
    }
    rep(i, h) rep(j, w) {
      if (dp[i][j] == INF)
        continue;
      ans = max(ans, dp[i][j]);
    }
  }

  cout << ans << endl;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> g(w);
  for (string &s : g)
    cin >> s;
  vector<int> dx = {-1, 0, 0, 1};
  vector<int> dy = {0, 1, -1, 0};
  int ans = 0;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (g[i][j] == '#')
        continue;
      int cnt = 0;
      vector<vector<int>> d(h, vector<int>(w, -1));
      queue<pair<int, int>> q;

      d[i][j] = 0;
      pair<int, int> p(i, j);
      q.push(p);

      while (!q.empty()) {
        pair<int, int> v = q.front();
        q.pop();
        int y = v.first, x = v.second;

        for (int k = 0; k < 4; k++) {
          int Y = y + dy[k], X = x + dx[k];

          if (0 <= Y && Y < h && 0 <= X && X < w && g[Y][X] == '.') {
            if (d[Y][X] != -1)
              continue;

            d[Y][X] = d[y][x] + 1;
            cnt = max(cnt, d[Y][X]);
            pair<int, int> V(Y, X);
            q.push(V);
          }
        }
      }
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> g(h);
  for (string &s : g)
    cin >> s;
  vector<int> dx = {-1, 0, 0, 1};
  vector<int> dy = {0, 1, -1, 0};
  int ans = 0;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (g[i][j] == '#')
        continue;
      int cnt = 0;
      vector<vector<int>> d(h, vector<int>(w, -1));
      queue<pair<int, int>> q;

      d[i][j] = 0;
      pair<int, int> p(i, j);
      q.push(p);

      while (!q.empty()) {
        pair<int, int> v = q.front();
        q.pop();
        int y = v.first, x = v.second;

        for (int k = 0; k < 4; k++) {
          int Y = y + dy[k], X = x + dx[k];

          if (0 <= Y && Y < h && 0 <= X && X < w && g[Y][X] == '.') {
            if (d[Y][X] != -1)
              continue;

            d[Y][X] = d[y][x] + 1;
            cnt = max(cnt, d[Y][X]);
            pair<int, int> V(Y, X);
            q.push(V);
          }
        }
      }
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, b, a) for (int i = (b)-1; i >= 0; --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = (n)-1; i >= 0; --i)
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define SORT(v) sort(v.begin(), v.end())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

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

  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  REP(i, H) cin >> S[i];
  int res = 0;
  int dh[] = {1, 0, -1, 0};
  int dw[] = {0, 1, 0, -1};
  REP(i, H) REP(j, H) {
    if (S[i][j] == '#')
      continue;
    vector<vector<int>> dis(H, vector<int>(W, -1));
    dis[i][j] = 0;
    queue<P> qu;
    qu.emplace(i, j);
    while (!qu.empty()) {
      int k, l;
      tie(k, l) = qu.front();
      qu.pop();
      REP(m, 4) {
        int y = k + dh[m];
        int x = l + dw[m];
        if (y >= 0 && y < H && x >= 0 && x < W && S[y][x] == '.' &&
            dis[y][x] < 0) {
          dis[y][x] = dis[k][l] + 1;
          res = max(res, dis[y][x]);
          qu.emplace(y, x);
        }
      }
    }
  }
  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, b, a) for (int i = (b)-1; i >= 0; --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = (n)-1; i >= 0; --i)
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define SORT(v) sort(v.begin(), v.end())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

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

  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  REP(i, H) cin >> S[i];
  int res = 0;
  int dh[] = {1, 0, -1, 0};
  int dw[] = {0, 1, 0, -1};
  REP(i, H) REP(j, W) {
    if (S[i][j] == '#')
      continue;
    vector<vector<int>> dis(H, vector<int>(W, -1));
    dis[i][j] = 0;
    queue<P> qu;
    qu.emplace(i, j);
    while (!qu.empty()) {
      int k, l;
      tie(k, l) = qu.front();
      qu.pop();
      REP(m, 4) {
        int y = k + dh[m];
        int x = l + dw[m];
        if (y >= 0 && y < H && x >= 0 && x < W && S[y][x] == '.' &&
            dis[y][x] < 0) {
          dis[y][x] = dis[k][l] + 1;
          res = max(res, dis[y][x]);
          qu.emplace(y, x);
        }
      }
    }
  }
  cout << res << endl;

  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <limits>
#include <queue>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define all(a) (a).begin(), (a).end()
#define clr(a, v) memset((a), (v), sizeof(a))

using ll = long long;
constexpr ll INF = std::numeric_limits<ll>::max() >> 2;
using P = std::pair<int, int>;
using St = std::pair<ll, P>;

int H, W;
char S[30][30];
ll dist[30][30];

const int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};

ll bfs(int si, int sj) {
  rep(i, 30) rep(j, 30) dist[i][j] = INF;

  dist[si][sj] = 0;

  std::queue<St> que;
  que.push(St(0, P(si, sj)));

  while (!que.empty()) {
    St st = que.front();
    que.pop();

    ll d = st.first;
    int i = st.second.first, j = st.second.second;

    dist[i][j] = d;

    rep(dir, 4) {
      int ni = i + dx[dir], nj = j + dy[dir];

      if (ni >= 0 && ni < H && nj >= 0 && nj < W && S[ni][nj] != '#' &&
          dist[ni][nj] == INF)
        que.push(St(d + 1, P(ni, nj)));
    }
  }

  return 0;
}

int main() {
  scanf("%d%d", &H, &W);

  rep(i, H) scanf("%s", S[i]);

  ll ans = 0;

  rep(si, H) rep(sj, W) if (S[si][sj] != '#') {
    bfs(si, sj);

    rep(gi, H) rep(gj, W) if (S[gi][gj] != '#' && dist[gi][gj] != INF) ans =
        std::max(ans, dist[gi][gj]);
  }

  printf("%lld\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <limits>
#include <queue>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define all(a) (a).begin(), (a).end()
#define clr(a, v) memset((a), (v), sizeof(a))

using ll = long long;
constexpr ll INF = std::numeric_limits<ll>::max() >> 2;
using P = std::pair<int, int>;
using St = std::pair<ll, P>;

int H, W;
char S[30][30];
ll dist[30][30];

const int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};

ll bfs(int si, int sj) {
  rep(i, 30) rep(j, 30) dist[i][j] = INF;

  dist[si][sj] = 0;

  std::queue<St> que;
  que.push(St(0, P(si, sj)));

  while (!que.empty()) {
    St st = que.front();
    que.pop();

    ll d = st.first;
    int i = st.second.first, j = st.second.second;

    if (i != si && j != sj && dist[i][j] != INF)
      continue;

    dist[i][j] = d;

    rep(dir, 4) {
      int ni = i + dx[dir], nj = j + dy[dir];

      if (ni >= 0 && ni < H && nj >= 0 && nj < W && S[ni][nj] != '#' &&
          dist[ni][nj] == INF)
        que.push(St(d + 1, P(ni, nj)));
    }
  }

  return 0;
}

int main() {
  scanf("%d%d", &H, &W);

  rep(i, H) scanf("%s", S[i]);

  ll ans = 0;

  rep(si, H) rep(sj, W) if (S[si][sj] != '#') {
    bfs(si, sj);

    rep(gi, H) rep(gj, W) if (S[gi][gj] != '#' && dist[gi][gj] != INF) ans =
        std::max(ans, dist[gi][gj]);
  }

  printf("%lld\n", ans);

  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <queue>
#include <stdio.h>
using namespace std;

int n, m;
char map[22][22];
int d[22][22];
int bfs(int sx, int sy) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      d[i][j] = -1;
  queue<pair<int, int>> q;
  q.push({sx, sy});
  d[sx][sy] = 0;
  int r = 0;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    r = d[x][y];
    int dx[4] = {0, 1, 0, -1};
    int dy[4] = {1, 0, -1, 0};
    for (int k = 0; k < 4; k++) {
      int px = x + dx[k], py = y + dy[k];
      if (px < 0 || px >= n || py < 0 || py >= m || map[px][py] == '#')
        continue;
      if (d[px][py] == -1) {
        q.push({px, py});
        d[px][py] = d[x][y] + 1;
      }
    }
  }
  return r;
}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%s", map[i]);
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (map[i][j] == '.') {
        int a = bfs(i, j);
        if (ans < a)
          ans = a;
      }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <queue>
#include <stdio.h>
using namespace std;

int n, m;
char map[22][22];
int d[22][22];
int bfs(int sx, int sy) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      d[i][j] = -1;
  queue<pair<int, int>> q;
  q.push({sx, sy});
  d[sx][sy] = 0;
  int r = 0;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    r = d[x][y];
    q.pop();
    int dx[4] = {0, 1, 0, -1};
    int dy[4] = {1, 0, -1, 0};
    for (int k = 0; k < 4; k++) {
      int px = x + dx[k], py = y + dy[k];
      if (px < 0 || px >= n || py < 0 || py >= m || map[px][py] == '#')
        continue;
      if (d[px][py] == -1) {
        q.push({px, py});
        d[px][py] = d[x][y] + 1;
      }
    }
  }
  return r;
}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%s", map[i]);
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (map[i][j] == '.') {
        int a = bfs(i, j);
        if (ans < a)
          ans = a;
      }
  printf("%d\n", ans);
  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define inf (int)(1e9)

int main() {
  int H, W, i, j, ans = 0;
  scanf("%d%d", &H, &W);
  vector<string> S(H);
  for (i = 0; i < H; i++) {
    cin >> S[i];
  }
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        continue;
      }
      vector<vector<int>> dis(H, vector<int>(W, inf));
      queue<pair<pair<int, int>, int>> q;
      q.push(make_pair(make_pair(i, j), 0));
      while (q.size() > 0) {
        int x = q.front().first.first;
        int y = q.front().first.second;
        int v = q.front().second;
        q.pop();
        if (x < 0 || H <= x || y < 0 || W <= y || S[x][y] == '#' ||
            dis[x][y] < v) {
          continue;
        }
        dis[x][y] = v;
        ans = max(ans, v);
        for (int d = 0; d < 4; d++) {
          q.push(make_pair(make_pair(x + dx[d], y + dy[d]), v + 1));
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define inf (int)(1e9)

int main() {
  int H, W, i, j, ans = 0;
  scanf("%d%d", &H, &W);
  vector<string> S(H);
  for (i = 0; i < H; i++) {
    cin >> S[i];
  }
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        continue;
      }
      vector<vector<int>> dis(H, vector<int>(W, inf));
      queue<pair<pair<int, int>, int>> q;
      q.push(make_pair(make_pair(i, j), 0));
      while (q.size() > 0) {
        int x = q.front().first.first;
        int y = q.front().first.second;
        int v = q.front().second;
        q.pop();
        if (x < 0 || H <= x || y < 0 || W <= y || S[x][y] == '#' ||
            dis[x][y] <= v) {
          continue;
        }
        dis[x][y] = v;
        ans = max(ans, v);
        for (int d = 0; d < 4; d++) {
          q.push(make_pair(make_pair(x + dx[d], y + dy[d]), v + 1));
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll h, w;
  cin >> h >> w;
  ll dx[4] = {1, 0, -1, 0};
  ll dy[4] = {0, 1, 0, -1};
  char ch[22][22];
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      cin >> ch[i][j];
    }
  }
  ll ans = 0;
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      ll cnt[22][22];
      for (ll i = 0; i < h; i++) {
        for (ll j = 0; j < w; j++) {
          cnt[i][j] = 0;
        }
      }
      if (ch[i][j] == '#') {
        continue;
      }
      cnt[i][j] = 1;
      queue<pair<ll, ll>> q;
      q.push(make_pair(i, j));
      while (q.size() != 0) {
        ll nowx = q.front().first;
        ll nowy = q.front().second;
        q.pop();
        for (ll i = 0; i < 4; i++) {
          ll nexx = nowx + dx[i];
          ll nexy = nowy + dy[i];
          if (ch[nexx][nexy] == '#') {
            continue;
          }
          if (cnt[nexx][nexy] != 0) {
            continue;
          }
          cnt[nexx][nexy] = cnt[nowx][nowy] + 1;
          q.push(make_pair(nexx, nexy));
        }
      }
      for (ll i = 0; i < h; i++) {
        for (ll j = 0; j < w; j++) {
          ans = max(ans, cnt[i][j]);
        }
      }
    }
  }
  cout << ans - 1;
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll h, w;
  cin >> h >> w;
  ll dx[4] = {1, 0, -1, 0};
  ll dy[4] = {0, 1, 0, -1};
  char ch[22][22];
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      cin >> ch[i][j];
    }
  }
  ll ans = 0;
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      ll cnt[22][22];
      for (ll i = 0; i < h; i++) {
        for (ll j = 0; j < w; j++) {
          cnt[i][j] = 0;
        }
      }
      if (ch[i][j] == '#') {
        continue;
      }
      cnt[i][j] = 1;
      queue<pair<ll, ll>> q;
      q.push(make_pair(i, j));
      while (q.size() != 0) {
        ll nowx = q.front().first;
        ll nowy = q.front().second;
        q.pop();
        for (ll i = 0; i < 4; i++) {
          ll nexx = nowx + dx[i];
          ll nexy = nowy + dy[i];
          if (nexx < 0 || nexx >= h || nexy < 0 || nexy >= w) {
            continue;
          }
          if (ch[nexx][nexy] == '#') {
            continue;
          }
          if (cnt[nexx][nexy] != 0) {
            continue;
          }
          cnt[nexx][nexy] = cnt[nowx][nowy] + 1;
          q.push(make_pair(nexx, nexy));
        }
      }
      for (ll i = 0; i < h; i++) {
        for (ll j = 0; j < w; j++) {
          ans = max(ans, cnt[i][j]);
        }
      }
    }
  }
  cout << ans - 1;
  // your code goes here
  return 0;
}