Problem p01214 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
vector<string> p, tmp, in;
void turn() {
  tmp = vector<string>(p[0].size(), "");
  rep(i, p[0].size()) rep(j, p.size()) tmp[i] += p[j][p[0].size() - i - 1];
  p = tmp;
}
int f() {
  int co = 0;
  rep(i, tmp.size()) {
    bool h = true;
    rep(j, tmp[i].size()) if (tmp[i][j] != '#') h = false;
    if (h)
      co++;
  }
  return co;
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n, m;
    cin >> n >> m;
    p = vector<string>(n);
    rep(i, n) cin >> p[i];
    int nx = inf, ny = inf, mx = -inf, my = -inf;
    rep(i, n) rep(j, m) if (p[i][j] == '#') {
      nx = min(nx, i);
      mx = max(mx, i);
      ny = min(ny, j);
      my = max(my, j);
    }
    if (nx == inf)
      return -1;
    tmp = vector<string>(0);
    loop(i, nx, mx + 1) { tmp.pb(p[i].substr(ny, my - ny + 1)); }
    p = tmp;
    cin >> n >> m;
    in = vector<string>(n);
    rep(i, n) cin >> in[i];
    tmp = in;
    int out = f();
    bool H = false;
    rep(w, 4) {
      turn();
      if (n - p.size() + 1 <= 0 || m - p[0].size() + 1 <= 0)
        continue;
      rep(i, n - p.size() + 1) rep(j, m - p[0].size() + 1) {
        tmp = in;
        bool h = true;
        rep(k, p.size()) rep(l, p[0].size()) {
          if (p[k][l] == '#' && in[i + k][j + l] == '#')
            h = false;
          else if (p[k][l] == '#')
            tmp[i + k][j + l] = '#';
        }
        if (!h)
          continue;
        out = max(out, f());
        H = true;
      }
    }
    if (H)
      cout << out << endl;
    else
      cout << -1 << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
vector<string> p, tmp, in;
void turn() {
  tmp = vector<string>(p[0].size(), "");
  rep(i, p[0].size()) rep(j, p.size()) tmp[i] += p[j][p[0].size() - i - 1];
  p = tmp;
}
int f() {
  int co = 0;
  rep(i, tmp.size()) {
    bool h = true;
    rep(j, tmp[i].size()) if (tmp[i][j] != '#') h = false;
    if (h)
      co++;
  }
  return co;
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n, m;
    cin >> n >> m;
    p = vector<string>(n);
    rep(i, n) cin >> p[i];
    int nx = inf, ny = inf, mx = -inf, my = -inf;
    rep(i, n) rep(j, m) if (p[i][j] == '#') {
      nx = min(nx, i);
      mx = max(mx, i);
      ny = min(ny, j);
      my = max(my, j);
    }
    if (nx == inf)
      return -1;
    tmp = vector<string>(0);
    loop(i, nx, mx + 1) { tmp.pb(p[i].substr(ny, my - ny + 1)); }
    p = tmp;
    cin >> n >> m;
    in = vector<string>(n);
    rep(i, n) cin >> in[i];
    tmp = in;
    int out = f();
    bool H = false;
    rep(w, 4) {
      turn();
      int aa = p.size(), bb = p[0].size();
      if (n - aa + 1 <= 0 || m - bb + 1 <= 0)
        continue;
      rep(i, n - p.size() + 1) rep(j, m - p[0].size() + 1) {
        tmp = in;
        bool h = true;
        rep(k, p.size()) rep(l, p[0].size()) {
          if (p[k][l] == '#' && in[i + k][j + l] == '#')
            h = false;
          else if (p[k][l] == '#')
            tmp[i + k][j + l] = '#';
        }
        if (!h)
          continue;
        out = max(out, f());
        H = true;
      }
    }
    if (H)
      cout << out << endl;
    else
      cout << -1 << endl;
  }
}