Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long

#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const ll mxx = 3e5 + 5;

int main() {
  fast int n, m;
  cin >> n >> m;
  int tot;
  cin >> tot;
  vector<pair<int, int>> v;
  map<pair<int, int>, int> mp;
  for (int i = 0; i < tot; i++) {
    int x, y;
    cin >> x >> y;
    mp[{x, y}] = 1;
    v.push_back({x, y});
  }

  int row[n + 1] = {}, col[m + 1] = {};
  int mxr = 0, mxc = 0;
  int indr, indc;
  for (int i = 0; i < tot; i++) {
    row[v[i].first]++;
    if (mxr < row[v[i].first]) {
      mxr = row[v[i].first];
      indr = v[i].first;
    }
    col[v[i].second]++;
    if (mxc < col[v[i].second]) {
      mxc = col[v[i].second];
      indc = v[i].second;
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int val = row[i] + mxc;
    if (mp[{i, indc}])
      val--;
    ans = max(ans, val);
  }

  for (int i = 1; i <= m; i++) {
    int val = col[i] + mxr;
    if (mp[{indr, i}])
      val--;
    ans = max(ans, val);
  }

  cout << ans;
}

Error: WA

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std::chrono;
using namespace std;
typedef long long ll;
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_mset                                                           \\
  tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,               \\
       tree_order_statistics_node_update>
#define fr(i, a, b) for (ll i = (a); i < (b); ++i)
#define frr(i, a, b) for (ll i = (a - 1); i >= (b); --i)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define ppb pop_back()
#define pf push_front
#define ppf pop_front()
#define F first
#define S second
#define pii pair<ll, ll>
#define rev(a) reverse(a.begin(), a.end())
#define srt(a) sort(a.begin(), a.end())
#define rsrt(a) sort(a.begin(), a.end(), greater<ll>())
#define sz(a) (ll) a.size()
#define endl "\\n"
#define po(a, b) (ll)(pow(a, b) + 0.5)
#define double long double
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl;
#define setbits(a) __builtin_popcount(a)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const ll M = 1e9 + 7;
const ll mxx = 1e6 + 5;
string grid[1001];
int n, m, cn, cm, fn, fm;
int vis[1001][1001];
bool done[1001][1001];
int ans;

bool in(int i, int j) {
  if (i < 0 || j < 0 || i >= n || j >= m)
    return false;
  return true;
}

void dfs(int i, int j, int move) {
  if (move >= ans)
    return;
  if (i == fn && j == fm) {
    ans = min(ans, move);
    return;
  }

  vis[i][j] = move;
  int a[4] = {};
  if (in(i + 1, j) && vis[i + 1][j] > move && grid[i + 1][j] == '.') {
    a[0] = 1;
    vis[i + 1][j] = move;
  }
  if (in(i, j + 1) && vis[i][j + 1] > move && grid[i][j + 1] == '.') {
    a[1] = 1;
    vis[i][j + 1] = move;
  }
  if (in(i - 1, j) && vis[i - 1][j] > move && grid[i - 1][j] == '.') {
    a[2] = 1;
    vis[i - 1][j] = move;
  }
  if (in(i, j - 1) && vis[i][j - 1] > move && grid[i][j - 1] == '.') {
    a[3] = 1;
    vis[i][j - 1] = move;
  }

  if (in(i + 1, j) && grid[i + 1][j] == '.' && a[0])
    dfs(i + 1, j, move);
  if (in(i, j + 1) && grid[i][j + 1] == '.' && a[1])
    dfs(i, j + 1, move);
  if (in(i - 1, j) && grid[i - 1][j] == '.' && a[2])
    dfs(i - 1, j, move);
  if (in(i, j - 1) && grid[i][j - 1] == '.' && a[3])
    dfs(i, j - 1, move);

  for (int ii = -2; ii <= 2; ii++) {
    for (int jj = -2; jj <= 2; jj++) {
      if (abs(ii) + abs(jj) <= 1)
        continue;
      if (in(i + ii, j + jj) && vis[ii + i][jj + j] > move + 1 &&
          grid[ii + i][jj + j] == '.')
        dfs(ii + i, jj + j, move + 1);
    }
  }
}
int main() {
  fast

          cin >>
      n >> m;
  cin >> cn >> cm;
  cin >> fn >> fm;
  fn--;
  fm--;
  cn--;
  cm--;
  for (int i = 0; i < n; i++)
    cin >> grid[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      vis[i][j] = M;
  memset(done, false, sizeof done);
  ans = M;
  vis[cn][cm] = 0;
  if (grid[fn][fm] == '#') {
    cout << -1;
    return 0;
  }
  dfs(cn, cm, 0);
  if (ans == M)
    cout << -1;
  else
    cout << ans;
}