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

#include <cstring>
#include <iostream>
using namespace std;
struct POS {
  int x;
  int y;
};
long long n, field[1001][1001], dp[1001][1001];
int h, w;
int main() {
  while (cin >> h >> w >> n, h || w || n) {
    memset(dp, 0, sizeof(dp));
    memset(field, 0, sizeof(field));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[j][i];
      }
    }
    dp[0][0] = n - 1;
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++) {
        if (dp[i][j] > 0) {
          if (dp[i][j] % 2 == 0) {
            dp[i + 1][j] += dp[i][j] / 2;
            dp[i][j + 1] += dp[i][j] / 2;
          } else {
            if (field[i][j] == 0) {
              dp[i + 1][j] += dp[i][j] / 2;
              dp[i][j + 1] += dp[i][j] / 2 + 1;
            }
            if (field[i][j] == 1) {
              dp[i + 1][j] += dp[i][j] / 2 + 1;
              dp[i][j + 1] += dp[i][j] / 2;
            }
          }
        }
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        field[j][i] = (field[j][i] + dp[j][i] % 2) % 2;
      }
    }
    int px = 0, py = 0;
    while (px < w && py < h) {
      if (field[px][py] == 0) {
        py++;
      }
      if (field[px][py] == 1) {
        px++;
      }
    }
    cout << py + 1 << " " << px + 1 << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
/*struct POS{
  int x;
  int y;
  };*/
// queue<POS> check;
long n, field[201][201]; //,next[201][201];
// bool flag[201][201];
int h, w;
int main() {
  while (cin >> h >> w >> n, h || w || n) {
    /*    for(int i=0; i<101; i++){
      for(int j=0; j<101; j++){
        next[j][i] = 0;
        flag[j][i] = false;
        field[i][j] = 0;
      }
      }*/
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[j][i];
      }
    }
    //    next[0][0] = n-1;
    //    POS a;
    //    a.x = 0;a.y = 0;check.push(a);flag[0][0] = true;
    /*    while(!check.empty()){
      POS p = check.front();
      if(p.x<w && p.y<h){
        if(next[p.x][p.y]>0){
          if(next[p.x][p.y]%2 == 0){
            next[p.x+1][p.y]+=next[p.x][p.y]/2;
            next[p.x][p.y+1]+=next[p.x][p.y]/2;
          }
          else{
            if(field[p.x][p.y] == 0){
              next[p.x+1][p.y]+=next[p.x][p.y]/2;
              next[p.x][p.y+1]+=next[p.x][p.y]/2+1;
            }
            else{
              next[p.x+1][p.y]+=next[p.x][p.y]/2+1;
              next[p.x][p.y+1]+=next[p.x][p.y]/2;
            }
          }
          a.x = p.x+1;a.y = p.y;
          if(!flag[a.x][a.y]){
            check.push(a);
            flag[a.x][a.y] = true;
          }
          a.x = p.x;a.y = p.y+1;
          if(!flag[a.x][a.y]){
            check.push(a);
            flag[a.x][a.y] = true;
          }
        }
      }
      check.pop();
      }*/
    /*    for(int i=0; i<h; i++){
      for(int j=0; j<w; j++){
        field[j][i] = (field[j][i]+next[j][i]%2)%2;
      }
      }*/
    int px = 0, py = 0;
    /*    while(px<w && py<h){
      if(field[px][py] == 0){py++;}
      if(field[px][py] == 1){px++;}
      }*/
    cout << py + 1 << " " << px + 1 << endl;
  }
  return 0;
}