Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

struct Point {
  int x;
  int y;
};

bool bmap[1000][1000] = {false};
bool smap[1000][1000] = {false};

void add_search(queue<Point> &search, int x, int y, int w, int h) {
  if (x < 0 || w <= x)
    return;
  if (y < 0 || h <= y)
    return;
  if (bmap[x][y])
    return;
  if (smap[x][y])
    return;

  // smap[x][y] = true;
  search.push({x, y});
}

int main() {
  int h, w;
  cin >> h >> w;

  queue<Point> search;
  FOR_LT(y, 0, h) {
    FOR_LT(x, 0, w) {
      char c;
      cin >> c;
      if (c == '#') {
        bmap[x][y] = true;
        add_search(search, x + 1, y, w, h);
        add_search(search, x - 1, y, w, h);
        add_search(search, x, y + 1, w, h);
        add_search(search, x, y - 1, w, h);
      }
    }
  }

  int count = 0;
  while (!search.empty()) {
    bool has_change = false;
    queue<Point> next_search;

    while (!search.empty()) {
      auto p = search.front();
      search.pop();

      int x = p.x;
      int y = p.y;
      if (!bmap[p.x][p.y]) {
        bmap[p.x][p.y] = true;
        has_change = true;
      }

      add_search(next_search, x + 1, y, w, h);
      add_search(next_search, x - 1, y, w, h);
      add_search(next_search, x, y + 1, w, h);
      add_search(next_search, x, y - 1, w, h);
    }

    search = next_search;
    if (has_change) {
      count++;
    }
  }

  cout << count << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

struct Point {
  int x;
  int y;
};

bool bmap[1000][1000] = {false};
bool smap[1000][1000] = {false};

void add_search(queue<Point> &search, int x, int y, int w, int h) {
  if (x < 0 || w <= x)
    return;
  if (y < 0 || h <= y)
    return;
  if (bmap[x][y])
    return;
  if (smap[x][y])
    return;

  smap[x][y] = true;
  search.push({x, y});
}

int main() {
  int h, w;
  cin >> h >> w;

  queue<Point> search;
  FOR_LT(y, 0, h) {
    FOR_LT(x, 0, w) {
      char c;
      cin >> c;
      if (c == '#') {
        bmap[x][y] = true;
        add_search(search, x + 1, y, w, h);
        add_search(search, x - 1, y, w, h);
        add_search(search, x, y + 1, w, h);
        add_search(search, x, y - 1, w, h);
      }
    }
  }

  int count = 0;
  while (!search.empty()) {
    bool has_change = false;
    queue<Point> next_search;

    while (!search.empty()) {
      auto p = search.front();
      search.pop();

      int x = p.x;
      int y = p.y;
      if (!bmap[p.x][p.y]) {
        bmap[p.x][p.y] = true;
        has_change = true;
      }

      add_search(next_search, x + 1, y, w, h);
      add_search(next_search, x - 1, y, w, h);
      add_search(next_search, x, y + 1, w, h);
      add_search(next_search, x, y - 1, w, h);
    }

    search = next_search;
    if (has_change) {
      count++;
    }
  }

  cout << count << endl;

  return 0;
}