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

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int INF = 1e9;

int d[51][51];

int main() {
  int tc;
  cin >> tc;
  rep(ij, 0, tc) {
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    rep(i, 0, h) { cin >> s[i]; }
    map<char, int> mp;
    int n = 0;
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (s[i][j] == '.')
          continue;
        if (!mp.count(s[i][j])) {
          mp[s[i][j]] = n++;
        }
      }
    }
    vector<int> yMIN(n, INF), yMAX(n, -1), xMIN(n, INF), xMAX(n, -1);
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (s[i][j] == '.')
          d[i][j] = -1;
        else {
          d[i][j] = mp[s[i][j]];
          chmin(yMIN[d[i][j]], i);
          chmax(yMAX[d[i][j]], i);
          chmin(xMIN[d[i][j]], j);
          chmax(xMAX[d[i][j]], j);
        }
      }
    }
    bool f = true;
    bool isPut[10][10] = {};
    rep(i, 0, n) {
      rep(j, yMIN[i], yMAX[i] + 1) {
        rep(k, xMIN[i], xMAX[i] + 1) {
          if (d[j][k] != i) {
            if (d[j][k] == -1)
              f = false;
            else {
              isPut[d[j][k]][i] = true;
            }
          }
        }
      }
    }
    if (!f) {
      cout << "SUSPICIOUS" << endl;
      continue;
    }
    f = false;
    vector<int> p(n);
    rep(i, 0, n) p[i] = i;
    do {
      bool g = true;
      rep(i, 0, n) {
        rep(j, 0, n) {
          if (isPut[i][j]) {
            if (p[i] < p[j])
              g = false;
          }
        }
      }
      if (g)
        f = true;
      // for(int ii = 0; ii < p.size(); ii++){
      //     if(ii) cout << ' ';
      //     cout << p[ii];
      // }
      // cout << endl;
    } while (next_permutation(p.begin(), p.end()));
    if (f) {
      cout << "SAFE" << endl;
    } else {
      cout << "SUSPICIOUS" << endl;
    }
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define ALL(a) a.begin(), a.end()
using namespace std;
typedef long long ll;

int n, w, h;
char field[100][100];
bool ok[100][100];
vector<char> vc;

bool check(int y, int x, char c) {
  bool f = 1, s = 1;
  int posx = x, posy = y, wide = 0;
  while (field[posy][posx] == c) {
    if (s == 1) {
      while (field[posy][posx] == c) {
        wide++;
        posy++;
      }
      s = 0;
    } else {
      int co = 0;
      while (field[posy][posx] == c) {
        co++;
        posy++;
      }
      if (co != wide)
        f = 0;
      while (ok[posy][posx]) {
        posy++;
        co++;
        if (co == wide) {
          f = 1;
          break;
        }
      }
    }
    posy = y;
    posx++;
  }
  if (f == 0)
    return false;
  rep(i, x, posx + 1) {
    rep(j, y, posy + 1) { ok[i][j] = 1; }
  }
  return true;
}

int main() {
  cin >> n;
  REP(q, n) {
    bool F[10];
    fill(F, F + 10, 1);
    bool f = 1;
    cin >> h >> w;
    REP(i, h) {
      string s;
      cin >> s;
      REP(j, w) {
        field[i][j] = s[j];
        if (field[i][j] != '.' && find(ALL(vc), field[i][j]) == vc.end()) {
          vc.push_back(field[i][j]);
        }
      }
    }
    int co = 0;
    while (1) {
      bool f2 = 0, nk = 0;
      REP(j, vc.size()) {
        if (F[j])
          break;
        if (j == vc.size() - 1)
          nk = 1;
      }
      if (nk)
        break;
      REP(j, h) {
        REP(k, w) {
          if (field[j][k] == vc[co]) {
            if (!check(j, k, vc[co]))
              f = 0;
            // else F[co] = 0;
            f2 = 1;
          }
          if (f2)
            break;
        }
        if (f2)
          break;
      }
      co++;
      if (co == vc.size())
        co = 0;
    }
    if (f)
      cout << "SAFE" << endl;
    else
      cout << "SUSPICIOUS" << endl;
    vc.clear();
  }
}