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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
int dx[6] = {-1, -1, 0, 1, 1, 0};
int dy[2][6] = {{-1, 0, 1, 0, -1, -1}, {0, 1, 1, 1, 0, -1}};
int W, H;
int m[105][105];
int f[105][105];
void bfs() {
  memset(f, 0, sizeof(f));
  queue<P> q;
  f[0][0] = 1;
  q.push(P(0, 0));
  while (!q.empty()) {
    P a = q.front();
    q.pop();
    for (int i = 0; i < 6; i++) {
      int nx = a.fi + dx[i], ny = a.sec + dy[a.fi % 2][i];
      if (nx < 0 || nx > 105 || ny < 0 || ny > 105)
        continue;
      if (f[nx][ny] || m[nx][ny])
        continue;
      q.push(P(nx, ny));
      f[nx][ny] = 1;
    }
  }
  return;
}
int count(int x, int y) {
  int res = 0;
  for (int i = 0; i < 6; i++) {
    int nx = x + dx[i], ny = y + dy[x % 2][i];
    if (nx < 0 || nx > 105 || ny < 0 || ny > 105)
      continue;
    if (f[nx][ny] != f[x][y])
      res++;
  }
  return res;
}
int main() {
  cin >> W >> H;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> m[i][j];
    }
  }
  bfs();
  int ans = 0;
  for (int i = 0; i <= H + 2; i++) {
    for (int j = 0; j <= W + 2; j++) {
      ans += count(i, j);
    }
  }
  ans /= 2;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size()
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
int f[105][105];
bool used[105][105];
int dx[2][6] = {{-1, -1, 0, 0, 1, 1}, {-1, -1, 0, 1, 1, 0}};
int dy[2][6] = {{-1, 0, -1, 1, -1, 0}, {0, 1, 1, 1, 0, -1}};
int W, H;
void bfs(int a, int b) {
  queue<P> q;
  q.push(P(a, b));
  vector<P> vec;
  bool flag = false;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (p.fi == 1 || p.fi == H || p.sec == 1 || p.sec == W)
      flag = true;
    vec.pb(p);
    used[p.fi][p.sec] = true;
    for (int i = 0; i < 6; i++) {
      int r = p.fi % 2;
      int nx = p.fi + dx[r][i], ny = p.sec + dy[r][i];
      if (nx <= 0 || nx > H || ny <= 0 || ny > W || used[nx][ny] ||
          f[nx][ny] == 1)
        continue;
      q.push(P(nx, ny));
    }
  }
  if (!flag) {
    for (int i = 0; i < vec.size(); i++) {
      f[vec[i].fi][vec[i].sec] = 1;
    }
  }
  return;
}
int count(int a, int b) {
  int res = 0;
  for (int i = 0; i < 6; i++) {
    int nx = a + dx[a % 2][i], ny = b + dy[a % 2][i];
    if (f[a][b] != f[nx][ny])
      res++;
  }
  return res;
}
int main() {
  memset(used, 0, sizeof(used));
  cin >> W >> H;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> f[i][j];
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (f[i][j] == 0 && !used[i][j])
        bfs(i, j);
    }
  }
  int ans = 0;
  for (int i = 0; i <= H + 1; i++) {
    for (int j = 0; j <= W + 1; j++) {
      ans += count(i, j);
    }
  }
  cout << ans / 2 << endl;
  return 0;
}