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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
vvi fil;
int r, l;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  int w, h;
  while (cin >> w >> h, w + h) {
    int n;
    cin >> n;
    vvi in(n, vi(4));
    vi x, y;
    rep(i, n) rep(j, 4) cin >> in[i][j];
    rep(k, 2) rep(i, n) for (int d = -1; d <= 1; d++) {
      int X = in[i][k * 2] + d, Y = in[i][k * 2 + 1] + d;
      if (0 <= X && X <= w)
        x.pb(X);
      if (0 <= Y && Y <= h)
        y.pb(Y);
    }
    sort(all(x));
    sort(all(y));
    x.erase(unique(all(x)), x.end());
    y.erase(unique(all(y)), y.end());
    r = x.size() - 1, l = y.size() - 1;
    fil = vvi(r, vi(l));
    rep(i, n) {
      int a = find(all(x), in[i][0]) - x.begin();
      int b = find(all(y), in[i][1]) - y.begin();
      int c = find(all(x), in[i][2]) - x.begin();
      int d = find(all(y), in[i][3]) - y.begin();
      loop(j, a, c) loop(k, b, d) fil[j][k] = 1;
    }
    int co = 0;
    //		rep(i,r){rep(j,l)cout<<fil[i][j];cout<<endl;}
    rep(i, r) rep(j, l) if (fil[i][j] == 0) {
      co++;
      fil[i][j] = 1;
      queue<pii> q;
      q.push(pii(i, j));
      while (!q.empty()) {
        pii p = q.front();
        q.pop();
        rep(k, 4) {
          int nx = p.first + dx[k];
          int ny = p.second + dy[k];
          if (nx < 0 || ny < 0 || nx >= r || ny >= l)
            continue;
          if (fil[nx][ny] == 0) {
            fil[nx][ny] = 1;
            q.push(pii(nx, ny));
          }
        }
      }
    }
    cout << co << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
vvi fil;
int r, l;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void dfs(int a, int b) {
  fil[a][b] = 1;
  rep(i, 4) {
    int nx = a + dx[i];
    int ny = b + dy[i];
    if (nx < 0 || ny < 0 || nx >= r || ny >= l)
      continue;
    if (fil[nx][ny] == 0)
      dfs(nx, ny);
  }
}
int main() {
  int w, h;
  while (cin >> w >> h, w + h) {
    int n;
    cin >> n;
    vvi in(n, vi(4));
    vi x, y;
    rep(i, n) rep(j, 4) cin >> in[i][j];
    rep(k, 2) rep(i, n) for (int d = -1; d <= 1; d++) {
      int X = in[i][k * 2] + d, Y = in[i][k * 2 + 1] + d;
      if (0 <= X && X <= w)
        x.pb(X);
      if (0 <= Y && Y <= h)
        y.pb(Y);
    }
    sort(all(x));
    sort(all(y));
    x.erase(unique(all(x)), x.end());
    y.erase(unique(all(y)), y.end());
    r = x.size() - 1, l = y.size() - 1;
    fil = vvi(r, vi(l));
    rep(i, n) {
      int a = find(all(x), in[i][0]) - x.begin();
      int b = find(all(y), in[i][1]) - y.begin();
      int c = find(all(x), in[i][2]) - x.begin();
      int d = find(all(y), in[i][3]) - y.begin();
      loop(j, a, c) loop(k, b, d) fil[j][k] = 1;
    }
    int co = 0;
    rep(i, r) rep(j, l) if (fil[i][j] == 0) {
      co++;
      dfs(i, j);
    }
    cout << co << endl;
  }
}