Problem p03361 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
typedef long long ll;

using namespace std;

template <typename T> void output(T, int);
template <typename T> T max(T, T);
int gcd(int a, int b);

int mo1[4] = {-1, 0, 0, 1};
int mo2[4] = {0, -1, 1, 0};
int h, w;
vector<string> s;
queue<pair<int, int>> q;

void del() {
  while (q.size()) {
    int y = q.front().first;
    int x = q.front().second;
    q.pop();
    s[y][x] = '.';

    rep(i, 4) {
      int ny = y + mo1[i];
      int nx = x + mo2[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w) {
        if (s[ny][nx] == '#') {
          q.push(make_pair(ny, nx));
        }
      }
    }
  }
}

int main() {
  // source
  cin >> h >> w;
  string temp;
  rep(i, h) {
    cin >> temp;
    s.push_back(temp);
  }

  rep(i, h) {
    rep(j, w) {
      if (s[i][j] == '#') {
        bool flag = true;
        rep(l, 4) {
          int y = i + mo1[l];
          int x = j + mo2[l];

          if (0 <= y && y < h && 0 <= x && x < w) {
            if (s[y][x] == '#') {
              flag = false;
              q.push(make_pair(y, x));
            }
          }
        }
        if (flag) {
          cout << "No" << endl;
          return 0;
        } else {
          del();
        }
      }
    }
  }

  cout << "Yes" << endl;
  return 0;
}

template <typename T> void output(T a, int precision) {
  if (precision > 0) {
    cout << setprecision(precision) << a << "\n";
  } else {
    cout << a << "\n";
  }
}

template <typename T> T max(T a, T b) {

  if (a > b) {
    return a;
  } else {
    return b;
  }
}

template <typename T> T min(T a, T b) {

  if (a < b) {
    return a;
  } else {
    return b;
  }
}

int gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
typedef long long ll;

using namespace std;

template <typename T> void output(T, int);
template <typename T> T max(T, T);
int gcd(int a, int b);

int mo1[4] = {-1, 0, 0, 1};
int mo2[4] = {0, -1, 1, 0};
int h, w;
vector<string> s;
queue<pair<int, int>> q;

void del() {
  while (q.size()) {
    int y = q.front().first;
    int x = q.front().second;
    q.pop();
    s[y][x] = '.';

    rep(i, 4) {
      int ny = y + mo1[i];
      int nx = x + mo2[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w) {
        if (s[ny][nx] == '#') {
          q.push(make_pair(ny, nx));
          s[ny][nx] = '.';
        }
      }
    }
  }
}

int main() {
  // source
  cin >> h >> w;
  string temp;
  rep(i, h) {
    cin >> temp;
    s.push_back(temp);
  }

  rep(i, h) {
    rep(j, w) {
      if (s[i][j] == '#') {
        bool flag = true;
        rep(l, 4) {
          int y = i + mo1[l];
          int x = j + mo2[l];

          if (0 <= y && y < h && 0 <= x && x < w) {
            if (s[y][x] == '#') {
              flag = false;
              q.push(make_pair(y, x));
            }
          }
        }
        if (flag) {
          cout << "No" << endl;
          return 0;
        } else {
          del();
        }
      }
    }
  }

  cout << "Yes" << endl;
  return 0;
}

template <typename T> void output(T a, int precision) {
  if (precision > 0) {
    cout << setprecision(precision) << a << "\n";
  } else {
    cout << a << "\n";
  }
}

template <typename T> T max(T a, T b) {

  if (a > b) {
    return a;
  } else {
    return b;
  }
}

template <typename T> T min(T a, T b) {

  if (a < b) {
    return a;
  } else {
    return b;
  }
}

int gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}