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

#include <bits/stdc++.h>

template <typename T> void fill_all(T &ar, const T &v) { ar = v; }
template <typename T, size_t N, typename U>
void fill_all(T (&ar)[N], const U &v) {
  for (auto &i : ar) {
    fill_all(i, v);
  }
}

int H, W;
char map[1000][1002];
char flag[2][3];

int main() {
  std::cin >> H >> W;

  for (int y = 0; y < H; ++y) {
    std::cin >> map[y];
  }
  for (int i = 0; i < 2; ++i) {
    std::cin >> flag[i];
  }
#ifdef DEBUG
  std::cout << "map32:" << map[2][3] << '\\n';
#endif
  auto is_same = [](int x, int y) {
    bool ok = true;
#ifdef DEBUG
    if (x == 3 && y == 1) {
      std::cout << "same:" << map[y][x] << '\\n';
    }
#endif
    for (int i = 0; i < 2; ++i)
      for (int j = 0; j < 2; ++j) {
        if (flag[j][i] == map[y + j][x + i]) {

        } else {
          ok = false;
#ifdef DEBUG
          if (x == 3 && y == 1) {
            std::cout << "samefalse:" << (x + i) << ' ' << (y + j) << ' '
                      << map[y + j][x + i] << ' ' << (int)map[y + j][x + i]
                      << '\\n';
          }
#endif
        }
      }
    return ok;
  };

  int32_t count = 0;
  for (int y = 0; y <= H - 2; ++y) {
    for (int x = 0; x <= W - 2; ++x) {
      if (is_same(x, y)) {
        ++count;
      }
    }
  }

  int32_t max = count;
  for (int y = 0; y < H; ++y) {
    for (int x = 0; x < W; ++x) {
      const char CHAR[] = "JOI";
      auto bef = map[y][x];
      auto bef_count = 0;
      for (int u = -1; u <= 0; ++u)
        for (int l = -1; l <= 0; ++l) {
          int lu_x = x + l, lu_y = y + u;
          if (lu_x < 0)
            continue;
          if (lu_y < 0)
            continue;
          if (W <= lu_x + 1)
            continue;
          if (H <= lu_y + 1)
            continue;

          if (is_same(lu_x, lu_y)) {
            ++bef_count;
          }
        }
#ifdef DEBUG
      std::cout << "IN" << x << ' ' << y << '\\n';
      std::cout << bef_count << std::endl;
#endif

      for (int char_ = 0; char_ < 3; ++char_) {
        map[y][x] = CHAR[char_];

#ifdef DEBUG
        if (x == 3 && y == 1) {
          std::cout << "MAP:" << map[y][x] << '\\n';
        }
#endif
        int add = 0;
        for (int u = -1; u <= 0; ++u)
          for (int l = -1; l <= 0; ++l) {
            int lu_x = x + l, lu_y = y + u;
            if (lu_x < 0)
              continue;
            if (lu_y < 0)
              continue;
            if (W <= lu_x + 1)
              continue;
            if (H <= lu_y + 1)
              continue;

            if (is_same(lu_x, lu_y)) {
              ++add;
            }
          }

#ifdef DEBUG
        std::cout << add << std::endl;
#endif
        max = std::max(max, count - bef_count + add);
      }
      map[y][x] = bef;
    }
  }

  std::cout << max << std::endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

template <typename T> void fill_all(T &ar, const T &v) { ar = v; }
template <typename T, size_t N, typename U>
void fill_all(T (&ar)[N], const U &v) {
  for (auto &i : ar) {
    fill_all(i, v);
  }
}

int H, W;
char map[1000][1002];
char flag[2][3];

int main() {
  std::cin >> H >> W;

  for (int y = 0; y < H; ++y) {
    std::cin >> map[y];
  }
  for (int i = 0; i < 2; ++i) {
    std::cin >> flag[i];
  }
#ifdef DEBUG
  std::cout << "map32:" << map[2][3] << '\\n';
#endif

  int32_t count = 0;
  for (int y = 0; y <= H - 2; ++y) {
    for (int x = 0; x <= W - 2; ++x) {
      bool ok = true;
      for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j) {
          if (flag[j][i] == map[y + j][x + i]) {

          } else {
            ok = false;
          }
        }
      if (ok) {
        ++count;
      }
    }
  }

  auto is_same = [](int x, int y) {
    bool ok = true;
#ifdef DEBUG
    if (x == 3 && y == 1) {
      std::cout << "same:" << map[y][x] << '\\n';
    }
#endif
    for (int i = 0; i < 2; ++i)
      for (int j = 0; j < 2; ++j) {
        if (flag[j][i] == map[y + j][x + i]) {

        } else {
          ok = false;
#ifdef DEBUG
          if (x == 3 && y == 1) {
            std::cout << "samefalse:" << (x + i) << ' ' << (y + j) << ' '
                      << map[y + j][x + i] << ' ' << (int)map[y + j][x + i]
                      << '\\n';
          }
#endif
        }
      }
    return ok;
  };

  int32_t max = count;
  for (int y = 0; y < H; ++y) {
    for (int x = 0; x < W; ++x) {
      const char CHAR[] = "JOI";
      auto bef = map[y][x];
      auto bef_count = 0;
      for (int u = -1; u <= 0; ++u)
        for (int l = -1; l <= 0; ++l) {
          int lu_x = x + l, lu_y = y + u;
          if (lu_x < 0)
            continue;
          if (lu_y < 0)
            continue;

          if (is_same(lu_x, lu_y)) {
            ++bef_count;
          }
        }
#ifdef DEBUG
      std::cout << "IN" << x << ' ' << y << '\\n';
      std::cout << bef_count << std::endl;
#endif

      for (int char_ = 0; char_ < 3; ++char_) {
        map[y][x] = CHAR[char_];

#ifdef DEBUG
        if (x == 3 && y == 1) {
          std::cout << "MAP:" << map[y][x] << '\\n';
        }
#endif
        int add = 0;
        for (int u = -1; u <= 0; ++u)
          for (int l = -1; l <= 0; ++l) {
            int lu_x = x + l, lu_y = y + u;
            if (lu_x < 0)
              continue;
            if (lu_y < 0)
              continue;

            if (is_same(lu_x, lu_y)) {
              ++add;
            }
          }

#ifdef DEBUG
        std::cout << add << std::endl;
#endif
        max = std::max(max, count - bef_count + add);
      }
      map[y][x] = bef;
    }
  }

  std::cout << max << std::endl;
}