Problem p03263 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

typedef pair<ll, ll> P;
long long int INF = 1e18;
double Pi = 3.141592653589;
long long int mod = 998244353;
// memset(a,0,sizeof(a)); →全部０にする

vector<ll> G[100005];
vector<P> tree[100010];
priority_queue<ll> pql;
priority_queue<P> pqp;
// big priority queue
priority_queue<ll, vector<ll>, greater<ll>> pqls;
priority_queue<P, vector<P>, greater<P>> pqps;
// small priority queue
// top pop

int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
// ↓,→,↑,←

#define p(x) cout << x << endl;
#define el cout << endl;
#define pe(x) cout << x << " ";
#define ps(x) cout << fixed << setprecision(25) << x << endl;
#define pu(x) cout << x;
#define re(i, a, b)                                                            \
  for (i = a; i <= b; i++)                                                     \
    ;
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout << #x << " = " << x << endl

ll rui(ll abc, ll bed) {
  // aのb乗を計算する
  if (bed == 0) {
    return 1;
  } else {
    ll ced = rui(abc, bed / 2);
    ced *= ced;
    ced %= mod;
    if (bed % 2 == 1) {
      ced *= abc;
      ced %= mod;
    }
    return ced;
  }
}

ll i, j, k, ii, jj;
ll n, m, num, sumxor, sum, ans;
ll a, b, c, d, e, f, g, h, w, v;
// ll x[800005],y[800005],z[900005];
ll d1[100005], d2[100005], d3[100005], d4[100005];
ll dp[1000][1000];

int main() {
  cin >> h >> w;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      cin >> dp[i][j];
    }
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (dp[i][j] % 2 == 1) {
        if (j < w - 1) {
          d1[num] = i;
          d2[num] = j;
          d3[num] = i;
          d4[num] = j + 1;
          dp[i][j]--;
          dp[i][j + 1]++;
          num++;
        } else if (i < h - 1) {
          d1[num] = i;
          d2[num] = j;
          d3[num] = i + 1;
          d4[num] = j;
          dp[i][j]--;
          dp[i + 1][j]++;
          num++;
        }
      }
    }
  }

  // assert(num <= h*w);
  p(num);
  for (i = 0; i < num; i++) {
    pe(d1[i] + 1);
    pe(d2[i] + 1);
    pe(d3[i] + 1);
    p(d4[i] + 1);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

typedef pair<ll, ll> P;
long long int INF = 1e18;
double Pi = 3.141592653589;
long long int mod = 998244353;
// memset(a,0,sizeof(a)); →全部０にする

vector<ll> G[100005];
vector<P> tree[100010];
priority_queue<ll> pql;
priority_queue<P> pqp;
// big priority queue
priority_queue<ll, vector<ll>, greater<ll>> pqls;
priority_queue<P, vector<P>, greater<P>> pqps;
// small priority queue
// top pop

int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
// ↓,→,↑,←

#define p(x) cout << x << endl;
#define el cout << endl;
#define pe(x) cout << x << " ";
#define ps(x) cout << fixed << setprecision(25) << x << endl;
#define pu(x) cout << x;
#define re(i, a, b)                                                            \
  for (i = a; i <= b; i++)                                                     \
    ;
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout << #x << " = " << x << endl

ll rui(ll abc, ll bed) {
  // aのb乗を計算する
  if (bed == 0) {
    return 1;
  } else {
    ll ced = rui(abc, bed / 2);
    ced *= ced;
    ced %= mod;
    if (bed % 2 == 1) {
      ced *= abc;
      ced %= mod;
    }
    return ced;
  }
}

ll i, j, k, ii, jj;
ll n, m, num, sumxor, sum, ans;
ll a, b, c, d, e, f, g, h, w, v;
// ll x[800005],y[800005],z[900005];
ll d1[500005], d2[500005], d3[500005], d4[500005];
ll dp[1000][1000];

int main() {
  cin >> h >> w;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      cin >> dp[i][j];
    }
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (dp[i][j] % 2 == 1) {
        if (j < w - 1) {
          d1[num] = i;
          d2[num] = j;
          d3[num] = i;
          d4[num] = j + 1;
          dp[i][j]--;
          dp[i][j + 1]++;
          num++;
        } else if (i < h - 1) {
          d1[num] = i;
          d2[num] = j;
          d3[num] = i + 1;
          d4[num] = j;
          dp[i][j]--;
          dp[i + 1][j]++;
          num++;
        }
      }
    }
  }

  // assert(num <= h*w);
  p(num);
  for (i = 0; i < num; i++) {
    pe(d1[i] + 1);
    pe(d2[i] + 1);
    pe(d3[i] + 1);
    p(d4[i] + 1);
  }

  return 0;
}