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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#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 all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<string> vs;
typedef vector<Def> vi;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
vs in, bl;
int n, m, out;
void f(int a, int b, vs NOW) {
  //	rep(i,3)cout<<in[i]<<endl;
  //	cout<<endl;
  if (a == m) {
    out = max(out, b);
    return;
  }
  for (int x = -1; x < 2; x++) {
    for (int y = -1; y < 2; y++) {
      vs now = NOW;
      bool h = true;
      rep(i, 2) rep(j, 2) rep(k, 2) {
        int dx = x + j;
        int dy = y + k;
        if (dx < 0 || dy < 0 || dx >= 2 || dy >= 2)
          ;
        else
          continue;
        if (bl[a][i * 4 + j * 2 + k] == '#')
          h = false;
      }
      if (h) {
        int pos = 0;
        for (int w = 30; w >= 0; w--) {
          bool h = true;
          rep(i, 2) rep(j, 2) rep(k, 2) {
            int dx = x + j, dy = y + k;
            if (dx < 0 || dy < 0 || dx >= 2 || dy >= 2)
              continue;
            if (bl[a][i * 4 + j * 2 + k] == '#' &&
                now[w + i][dx * 2 + dy] == '#')
              h = false;
          }

          if (!h) {
            pos = w + 1;
            break;
          }
        }
        rep(i, 2) rep(j, 2) rep(k, 2) {
          int dx = x + j, dy = y + k;
          if (dx < 0 || dy < 0 || dx >= 2 || dy >= 2)
            continue;
          if (bl[a][i * 4 + j * 2 + k] == '#') {
            now[pos + i][dx * 2 + dy] = '#';
          }
        }
        int co = 0;
        loop(i, 1, now.size()) {
          if (now[i] == "####") {
            co++;
            now.erase(now.begin() + i);
            i--;
          }
        }
        f(a + 1, b + co, now);
      }
    }
  }
}
int main() {
  while (cin >> n >> m, n) {
    in = vs(40, "....");
    in[0] = "####";
    rep(i, n) {
      string a, b;
      cin >> a >> b;
      in[i + 1] = a + b;
    }
    bl = vs(m);
    rep(i, m) {
      string a, b, c, d;
      cin >> a >> b >> c >> d;
      bl[i] = a + b + c + d;
    }
    out = 0;
    f(0, 0, in);
    cout << out << endl;
  }
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#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 all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<string> vs;
typedef vector<Def> vi;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
vs in, bl;
int n, m, out;
void f(int a, int b, vs NOW) {
  //	rep(i,3)cout<<in[i]<<endl;
  //	cout<<endl;
  if (a == m) {
    out = max(out, b);
    return;
  }
  for (int x = -1; x < 2; x++) {
    for (int y = -1; y < 2; y++) {
      vs now = NOW;
      bool h = true;
      rep(i, 2) rep(j, 2) rep(k, 2) {
        int dx = x + j;
        int dy = y + k;
        if (dx < 0 || dy < 0 || dx >= 2 || dy >= 2)
          ;
        else
          continue;
        if (bl[a][i * 4 + j * 2 + k] == '#')
          h = false;
      }
      if (h) {
        int pos = 0;
        for (int w = 18; w >= 0; w--) {
          bool h = true;
          rep(i, 2) rep(j, 2) rep(k, 2) {
            int dx = x + j, dy = y + k;
            if (dx < 0 || dy < 0 || dx >= 2 || dy >= 2)
              continue;
            if (bl[a][i * 4 + j * 2 + k] == '#' &&
                now[w + i][dx * 2 + dy] == '#')
              h = false;
          }

          if (!h) {
            pos = w + 1;
            break;
          }
        }
        rep(i, 2) rep(j, 2) rep(k, 2) {
          int dx = x + j, dy = y + k;
          if (dx < 0 || dy < 0 || dx >= 2 || dy >= 2)
            continue;
          if (bl[a][i * 4 + j * 2 + k] == '#') {
            now[pos + i][dx * 2 + dy] = '#';
          }
        }
        int co = 0;
        loop(i, 1, now.size()) {
          if (now[i] == "####") {
            co++;
            now.erase(now.begin() + i);
            i--;
          }
        }
        f(a + 1, b + co, now);
      }
    }
  }
}
int main() {
  while (cin >> n >> m, n) {
    in = vs(20, "....");
    in[0] = "####";
    rep(i, n) {
      string a, b;
      cin >> a >> b;
      in[i + 1] = a + b;
    }
    bl = vs(m);
    rep(i, m) {
      string a, b, c, d;
      cin >> a >> b >> c >> d;
      bl[i] = a + b + c + d;
    }
    out = 0;
    f(0, 0, in);
    cout << out << endl;
  }
}