Problem p00481 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
int main() {
  int h, w, n, px, py, ans;
  int field[100][100];
  int ch[100][100];
  while (cin >> h >> w >> n) {
    ans = 0;
    string s;
    for (int i = 0; i < h; i++) {
      cin >> s;
      for (int j = 0; j < w; j++) {
        if (s[j] == 'S') {
          px = j;
          py = i;
          field[j][i] = 0;
        } else if (s[j] >= '1' && s[j] <= '9') {
          field[j][i] = s[j] - '0';
        } else if (s[j] == 'X') {
          field[j][i] = -1;
        } else {
          field[j][i] = 0;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      queue<pair<int, int>> play;
      play.push(make_pair(px, py));
      for (int j = 0; j < h; j++) {
        for (int k = 0; k < w; k++) {
          ch[k][j] = 0;
        }
      }
      ch[px][py] = 1;
      for (;;) {
        int x = play.front().first;
        int y = play.front().second;
        if (field[x][y] == i + 1) {
          ans += ch[x][y] - 1;
          px = x;
          py = y;
          break;
        }
        if (x > 0) {
          if (field[x - 1][y] != -1) {
            if (ch[x - 1][y] == 0) {
              play.push(make_pair(x - 1, y));
              ch[x - 1][y] = ch[x][y] + 1;
            }
          }
        }
        if (y > 0) {
          if (field[x][y - 1] != -1) {
            if (ch[x][y - 1] == 0) {
              play.push(make_pair(x, y - 1));
              ch[x][y - 1] = ch[x][y] + 1;
            }
          }
        }
        if (x < w - 1) {
          if (field[x + 1][y] != -1) {
            if (ch[x + 1][y] == 0) {
              play.push(make_pair(x + 1, y));
              ch[x + 1][y] = ch[x][y] + 1;
            }
          }
        }
        if (y < h - 1) {
          if (field[x][y + 1] != -1) {
            if (ch[x][y + 1] == 0) {
              play.push(make_pair(x, y + 1));
              ch[x][y + 1] = ch[x][y] + 1;
            }
          }
        }
        play.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
int main() {
  int h, w, n, px, py, ans;
  int field[1000][1000];
  int ch[1000][1000];
  while (cin >> h >> w >> n) {
    ans = 0;
    string s;
    for (int i = 0; i < h; i++) {
      cin >> s;
      for (int j = 0; j < w; j++) {
        if (s[j] == 'S') {
          px = j;
          py = i;
          field[j][i] = 0;
        } else if (s[j] >= '1' && s[j] <= '9') {
          field[j][i] = s[j] - '0';
        } else if (s[j] == 'X') {
          field[j][i] = -1;
        } else {
          field[j][i] = 0;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      queue<pair<int, int>> play;
      play.push(make_pair(px, py));
      for (int j = 0; j < h; j++) {
        for (int k = 0; k < w; k++) {
          ch[k][j] = 0;
        }
      }
      ch[px][py] = 1;
      for (;;) {
        int x = play.front().first;
        int y = play.front().second;
        if (field[x][y] == i + 1) {
          ans += ch[x][y] - 1;
          px = x;
          py = y;
          break;
        }
        if (x > 0) {
          if (field[x - 1][y] != -1) {
            if (ch[x - 1][y] == 0) {
              play.push(make_pair(x - 1, y));
              ch[x - 1][y] = ch[x][y] + 1;
            }
          }
        }
        if (y > 0) {
          if (field[x][y - 1] != -1) {
            if (ch[x][y - 1] == 0) {
              play.push(make_pair(x, y - 1));
              ch[x][y - 1] = ch[x][y] + 1;
            }
          }
        }
        if (x < w - 1) {
          if (field[x + 1][y] != -1) {
            if (ch[x + 1][y] == 0) {
              play.push(make_pair(x + 1, y));
              ch[x + 1][y] = ch[x][y] + 1;
            }
          }
        }
        if (y < h - 1) {
          if (field[x][y + 1] != -1) {
            if (ch[x][y + 1] == 0) {
              play.push(make_pair(x, y + 1));
              ch[x][y + 1] = ch[x][y] + 1;
            }
          }
        }
        play.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}