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

#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forr(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fora(i, a, b) for (int i = int(a); i <= int(b); i++)
#define forb(i, a, b) for (int i = int(a); i >= int(b); i--)
#define mod 1000000007
using namespace std;
using ll = long long int;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<ll> vii;
typedef pair<ll, ll> pii;
template <typename T> string ts(T v) {
  string res = "{";
  bool r = true;
  for (const auto &x : v) {
    if (!r)
      res += ",";
    r = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
vi v;
int n, m, k;
char grid[10][10];
vector<vi> perRows, perCols;
void go(vi c, int cur, bool r) {
  if (cur == (r == 0 ? m : n)) {
    if (r)
      perRows.pb(c);
    else
      perCols.pb(c);
    return;
  }
  c.pb(v[cur]);
  go(c, cur + 1, r);
  c.pop_back();
  go(c, cur + 1, r);
}
int get(vi rows, vi cols) {
  bool done[n][m];
  forn(i, n) forn(j, m) done[i][j] = 1;
  for (auto i : rows) {
    forn(j, m) done[i][j] = 0;
  }
  for (auto i : cols) {
    forn(j, n) done[j][i] = 0;
  }
  int ans = 0;
  forn(i, n) {
    forn(j, m) {
      if (grid[i][j] == '#' && done[i][j])
        ans++;
    }
  }
  return ans;
}
void solve() {
  vi c;
  cin >> n >> m >> k;
  forn(i, n) v.pb(i);
  go(c, 0, 1);
  v.clear();
  c.clear();
  forn(i, m) v.pb(i);
  go(c, 0, 0);
  forn(i, n) {
    forn(j, m) { cin >> grid[i][j]; }
  }
  int x = perRows.size(), y = perCols.size();
  int ans = 0;
  forn(i, x) {
    forn(j, y) {
      vi a = perRows[i], b = perCols[j];
      if (get(a, b) == k)
        ans++;
    }
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  //	cin>>t;
  while (t--)
    solve();
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forr(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fora(i, a, b) for (int i = int(a); i <= int(b); i++)
#define forb(i, a, b) for (int i = int(a); i >= int(b); i--)
#define mod 1000000007
using namespace std;
using ll = long long int;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<ll> vii;
typedef pair<ll, ll> pii;
template <typename T> string ts(T v) {
  string res = "{";
  bool r = true;
  for (const auto &x : v) {
    if (!r)
      res += ",";
    r = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
vi x;
char grid[10][10];
int get(int n, int m, int x, int y) {
  bool done[n][m];
  forn(i, n) { forn(j, m) done[i][j] = 0; }
  int total = 0;
  forn(i, n) {
    forn(j, m) {
      if (grid[i][j] == '#')
        total++;
    }
  }
  int red = 0;
  if (x != -1) {
    forn(i, m) {
      if (grid[x][i] == '#') {
        done[x][i] = 1;
        red++;
      }
    }
  }
  if (y != -1) {
    forn(i, n) {
      if (grid[i][y] == '#' && !done[i][y])
        red++;
    }
  }
  // if(x!=-1&&y!=-1&&grid[x][y]=='#')red--;
  return total - red;
}
int get2(int n, int m, int x, int y) {
  bool done[n][m];
  forn(i, n) { forn(j, m) done[i][j] = 0; }
  int total = 0;
  forn(i, n) {
    forn(j, m) {
      if (grid[i][j] == '#')
        total++;
    }
  }
  int red = 0;
  if (x != -1) {
    forn(i, m) {
      if (grid[x][i] == '#') {
        done[x][i] = 1;
        red++;
      }
    }
  }
  if (y != -1) {
    forn(i, m) {
      if (grid[y][i] == '#' && !done[y][i])
        red++;
    }
  }
  // if(x!=-1&&y!=-1&&grid[x][y]=='#')red--;
  return total - red;
}
int get3(int n, int m, int x, int y) {
  bool done[n][m];
  forn(i, n) { forn(j, m) done[i][j] = 0; }
  int total = 0;
  forn(i, n) {
    forn(j, m) {
      if (grid[i][j] == '#')
        total++;
    }
  }
  int red = 0;
  if (x != -1) {
    forn(i, n) {
      if (grid[i][y] == '#') {
        done[x][i] = 1;
        red++;
      }
    }
  }
  if (y != -1) {
    forn(i, n) {
      if (grid[i][y] == '#' && !done[i][y])
        red++;
    }
  }
  // if(x!=-1&&y!=-1&&grid[x][y]=='#')red--;
  return total - red;
}
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  int b = 0;
  set<pi> s;
  forn(i, n) forn(j, m) cin >> grid[i][j];
  for (int i = -1; i < n; i++) {
    for (int j = -1; j < m; j++) {
      int npainted = get(n, m, i, j);
      // cout<<npainted<<endl;
      if (npainted == k) {
        // cout<<i<<" "<<j<<endl;
        s.insert({i, j});
      }
    }
  }
  for (int i = -1; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int npainted = get2(n, m, i, j);
      // cout<<npainted<<endl;
      if (npainted == k) {
        // cout<<i<<" "<<j<<endl;
        if (!s.count({i, j}) && !s.count({j, i}))
          s.insert({i, j});
      }
    }
  }
  for (int i = -1; i < m; i++) {
    for (int j = 0; j < m; j++) {
      int npainted = get3(n, m, i, j);
      // cout<<npainted<<endl;
      if (npainted == k) {
        // cout<<i<<" "<<j<<endl;
        if (!s.count({i, j}) && !s.count({j, i}))
          s.insert({i, j});
      }
    }
  }
  cout << s.size();
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}