Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define mp make_pair
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define pii pair<ll, ll>
#define pb push_back
#define F first
#define S second
#define mi map<ll, ll>
#define mii map<pii, ll>
const ll N = (1e7 + 1); // mod=998244353;
ll n, m, k;             //
ll a[N], dp[N];
string s;
ll l = (1e7 + 2);
char ch[1000][1000];
int h, w, c = 0;
// bool vis[1000][1000]={0}
bool valid(int p, int q) {
  if (p < 0 || p >= h)
    return 0;
  if (q < 0 || q >= w)
    return 0;
  if (ch[p][q] == '#') {
    return 0;
  }
  return 1;
}
int main() {
  cin >> h >> w;
  queue<pii> q, q1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> ch[i][j];
      if (ch[i][j] == '#') {
        q.push(mp(i, j));
        // vis[i][j]=1;
        c++;
      }
    }
  }
  int ans = 0;
  while (c < h * w) {
    // cout<<"c="<<c<<endl;
    ans++;
    while (q.size()) {
      int x = q.front().F, y = q.front().S;
      q.pop();
      if (valid(x + 1, y)) {
        ch[x + 1][y] = '#';
        q1.push(mp(x + 1, y));
        c++;
      }
      if (valid(x - 1, y)) {
        ch[x - 1][y] = '#';
        q1.push(mp(x - 1, y));
        c++;
      }
      if (valid(x, y + 1)) {
        ch[x][y + 1] = '#';
        q1.push(mp(x, y));
        c++;
      }
      if (valid(x, y - 1)) {
        ch[x][y - 1] = '#';
        q1.push(mp(x, y - 1));
        c++;
      }
    }
    q = q1;
    while (q1.size())
      q1.pop();
  }
  cout << ans << endl;
}
/*
2
5 2
3 6 4 7 2
5 3
3 -5 6 3 10


*/


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define mp make_pair
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define pii pair<ll, ll>
#define pb push_back
#define F first
#define S second
#define mi map<ll, ll>
#define mii map<pii, ll>
const ll N = (1e7 + 1); // mod=998244353;
ll n, m, k;             //
ll a[N], dp[N];
string s;
ll l = (1e7 + 2);
char ch[1000][1000];
int h, w, c = 0;
// bool vis[1000][1000]={0}
bool valid(int p, int q) {
  if (p < 0 || p >= h)
    return 0;
  if (q < 0 || q >= w)
    return 0;
  if (ch[p][q] == '#') {
    return 0;
  }
  return 1;
}
int main() {
  cin >> h >> w;
  queue<pii> q, q1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> ch[i][j];
      if (ch[i][j] == '#') {
        q.push(mp(i, j));
        // vis[i][j]=1;
        c++;
      }
    }
  }
  int ans = 0;
  while (c < h * w) {
    // cout<<"c="<<c<<endl;
    ans++;
    while (q.size()) {
      int x = q.front().F, y = q.front().S;
      q.pop();
      if (valid(x + 1, y)) {
        ch[x + 1][y] = '#';
        q1.push(mp(x + 1, y));
        c++;
      }
      if (valid(x - 1, y)) {
        ch[x - 1][y] = '#';
        q1.push(mp(x - 1, y));
        c++;
      }
      if (valid(x, y + 1)) {
        ch[x][y + 1] = '#';
        q1.push(mp(x, y + 1));
        c++;
      }
      if (valid(x, y - 1)) {
        ch[x][y - 1] = '#';
        q1.push(mp(x, y - 1));
        c++;
      }
    }
    q = q1;
    while (q1.size())
      q1.pop();
  }
  cout << ans << endl;
}
/*
2
5 2
3 6 4 7 2
5 3
3 -5 6 3 10


*/