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

#include <iostream>
#include <queue>
using namespace std;

int G[410];
int R[410][410];
int D[410];
int bfs(int src, int num);

int main() {
  int W, H, root;
  char c;

  while (true) {
    for (int i = 0; i < 400; i++) {
      for (int j = 0; j < 400; j++) {
        R[i][j] = 0;
      }
    }
    for (int i = 0; i < 400; i++) {
      G[i] = 0;
    }
    for (int i = 0; i < 400; i++) {
      D[i] = 100;
    }
    cin >> W >> H;
    if ((W == 0) && (H == 0))
      break;
    for (int i = 0; i < W * H; i++) {
      cin >> c;
      if (c == '.')
        G[i] = 1;
      if (c == '#')
        G[i] = 0;
      if (c == '@') {
        G[i] = 1;
        root = i;
      }
    }
    for (int i = 0; i < W * H; i++) {
      if (G[i] == 1) {
        if (i % W != 0) {      //????????????
          if (G[i - 1] == 1) { //???????????????
            R[i][i - 1] = 1;
          }
        }
        if (i % W != W - 1) {  //????????????
          if (G[i + 1] == 1) { //???????????????
            R[i][i + 1] = 1;
          }
        }
        if (i > W - 1) {       //???????????????
          if (G[i - W] == 1) { //???????????????
            R[i][i - W] = 1;
          }
        }
        if (i < W * H - W) { //???????????????
          if (G[i + W] == 1) {
            R[i][i + W] = 1;
          }
        }
      }
    }
    cout << bfs(root, W * H) << endl;
  }
  /*
  for(int i=0;i<99;i++) {
          cout << i << " " << G[i] << endl;
  }
  cout << G[98] << endl;
  cout << R[87][98] << endl;
  */
}

int bfs(int src, int num) {
  int cur, dst;
  int count = 1;
  // cerr << "bfs root = " << src << endl;
  queue<int> Q; // ??´??°????????????????????\\????????????
  Q.push(src);
  D[src] = 0; // ?????????
  while (!Q.empty()) {

    int cur = Q.front(); // ??????????´????????????????
    Q.pop();
    // ???????¢??????¨??¨???
    // cerr << cur << " " << D[cur] << endl;
    for (int dst = 0; dst < num; dst++) { // ???????????? dst ????????????
      if ((R[cur][dst] == 1) &&
          (D[dst] ==
           100)) { // cur ?????? dst ??????????????????dst ???????¨??????????
        D[dst] = D[cur] + 1; //
        Q.push(dst);         // dst ????¨???????????????????
        count++;
      }
    }
  }
  return count;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;

int G[1010];
int R[110][110];
int D[1010];
int bfs(int src, int num);

int main() {
  int W, H, root;
  char c;

  while (true) {
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        R[i][j] = 0;
      }
    }
    for (int i = 0; i < 1000; i++) {
      G[i] = 0;
    }
    for (int i = 0; i < 1000; i++) {
      D[i] = 100;
    }
    cin >> W >> H;
    if ((W == 0) && (H == 0))
      break;
    for (int i = 0; i < W * H; i++) {
      cin >> c;
      if (c == '.')
        G[i] = 1;
      if (c == '#')
        G[i] = 0;
      if (c == '@') {
        G[i] = 1;
        root = i;
      }
    }
    for (int i = 0; i < W * H; i++) {
      if (G[i] == 1) {
        if (i % W != 0) {      //????????????
          if (G[i - 1] == 1) { //???????????????
            R[i][i - 1] = 1;
          }
        }
        if (i % W != W - 1) {  //????????????
          if (G[i + 1] == 1) { //???????????????
            R[i][i + 1] = 1;
          }
        }
        if (i > W - 1) {       //???????????????
          if (G[i - W] == 1) { //???????????????
            R[i][i - W] = 1;
          }
        }
        if (i < W * H - W) { //???????????????
          if (G[i + W] == 1) {
            R[i][i + W] = 1;
          }
        }
      }
    }
    cout << bfs(root, W * H) << endl;
  }
  /*
  for(int i=0;i<99;i++) {
          cout << i << " " << G[i] << endl;
  }
  cout << G[98] << endl;
  cout << R[87][98] << endl;
  */
}

int bfs(int src, int num) {
  int cur, dst;
  int count = 1;
  // cerr << "bfs root = " << src << endl;
  queue<int> Q; // ??´??°????????????????????\\????????????
  Q.push(src);
  D[src] = 0; // ?????????
  while (!Q.empty()) {

    int cur = Q.front(); // ??????????´????????????????
    Q.pop();
    // ???????¢??????¨??¨???
    // cerr << cur << " " << D[cur] << endl;
    for (int dst = 0; dst < num; dst++) { // ???????????? dst ????????????
      if ((R[cur][dst] == 1) &&
          (D[dst] ==
           100)) { // cur ?????? dst ??????????????????dst ???????¨??????????
        D[dst] = D[cur] + 1; //
        Q.push(dst);         // dst ????¨???????????????????
        count++;
      }
    }
  }
  return count;
}