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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef long double db;
#define pb push_back
#define mp make_pair
#define ub(v, val) upper_bound(v.begin(), v.end(), val)
#define np(str) next_permutation(str.begin(), str.end())
#define lb(v, val) lower_bound(v.begin(), v.end(), val)
#define sortv(vec) sort(vec.begin(), vec.end())
#define rev(p) reverse(p.begin(), p.end());
#define v vector
#define PI 3.1415926535
#define len length()
#define repc(i, s, e) for (ll i = s; i < e; i++)
#define fi first
#define se second
#define mset(a, val) memset(a, val, sizeof(a));
#define mt make_tuple
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const ll M = 1e9 + 7;
ll INF = 1e18 + 1;

ll n, m;

int main() {
  // your code goes here
  IOS;

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll i, j, k, t, x, y, z, N;
  cin >> n >> m >> x;
  map<ll, ll, greater<ll>> r;
  map<ll, ll, greater<ll>> c;
  map<pair<ll, ll>, ll> el;
  rep(i, x) {
    cin >> y >> z;
    r[y]++;
    c[z]++;
    el[{y, z}] = 1;
  }
  set<pair<ll, ll>, greater<pair<ll, ll>>> row, col;
  for (auto u : r) {
    row.insert({u.se, u.fi});
  }
  for (auto u : c) {
    col.insert({u.se, u.fi});
  }
  ll mx1 = (*row.begin()).fi;
  ll mx2 = (*col.begin()).fi;
  ll flg = 0;
  for (auto r1 : row) {
    if (r1.fi < mx1)
      break;
    for (auto c1 : col) {
      if (c1.fi < mx2) {
        break;
      }
      if (el[{r1.se, c1.se}] == 0) {
        flg = 1;
        break;
      }
    }
    if (flg)
      break;
  }

  if (flg) {
    cout << mx1 + mx2;
  } else
    cout << mx1 + mx2 - 1;

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int ll;
typedef long double db;
#define pb push_back
#define mp make_pair
#define ub(v, val) upper_bound(v.begin(), v.end(), val)
#define np(str) next_permutation(str.begin(), str.end())
#define lb(v, val) lower_bound(v.begin(), v.end(), val)
#define sortv(vec) sort(vec.begin(), vec.end())
#define rev(p) reverse(p.begin(), p.end());
#define v vector
#define PI 3.1415926535
#define len length()
#define repc(i, s, e) for (ll i = s; i < e; i++)
#define fi first
#define se second
#define mset(a, val) memset(a, val, sizeof(a));
#define mt make_tuple
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const ll M = 1e9 + 7;
ll INF = 1e18;

ll n, m;
v<v<char>> s;
v<v<ll>> used;
v<v<ll>> warp;
v<v<ll>> con;
void dfs2(ll i, ll j, ll l, ll r) {
  con[i - (l - 2)][j - (r - 2)] = 1;
  v<pair<ll, ll>> move = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  for (auto mv : move) {
    ll x = i + mv.fi;
    ll y = j + mv.se;
    if (x >= l - 2 && x >= 0 && x < n && y >= 0 && y < m && x <= l + 2 &&
        y >= r - 2 && y <= r + 2) {
      if (s[x][y] == '.' && con[x - (l - 2)][y - (r - 2)] == 0) {
        dfs2(x, y, l, r);
      }
    }
  }
}
ll recur(ll i, ll j) {
  used[i][j] = 1;
  ll x, y;
  if (warp[i][j] == -1) {
    ll mn = INF;
    con.assign(5, v<ll>(5, 0));
    dfs2(i, j, i, j);
    for (x = i - 2; x <= i + 2; x++) {
      for (y = j - 2; y <= j + 2; y++) {
        if (x >= 0 && x < n && y >= 0 && y < m) {
          if (s[x][y] == '.' && used[x][y] == 0) {
            if (con[x - (i - 2)][y - (j - 2)] == 1) {
              mn = min(mn, recur(x, y));
            } else {
              mn = min(mn, 1LL + recur(x, y));
            }
          }
        }
      }
    }
    if (mn < INF)
      warp[i][j] = mn;
    else
      warp[i][j] = INF;
  }
  return warp[i][j];
}

int main() {
  // your code goes here
  IOS;

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll i, j, t, k, x, y, z, N;
  cin >> n >> m;
  ll x1, x2, y1, y2;
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  warp.assign(n, v<ll>(m, -1));
  s.assign(n, v<char>(m, '$'));
  used.assign(n, v<ll>(m, 0));
  rep(i, n) {
    rep(j, m) { cin >> s[i][j]; }
  }
  warp[x1 - 1][y1 - 1] = 0;
  x = recur(x2 - 1, y2 - 1);
  if (x == INF)
    cout << -1;
  else {
    cout << x;
  }

  return 0;
}