Problem p03792 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define pb push_back
#define mp make_pair
#define boost ios_base::sync_with_stdio(false)
#define eb emplace_back
#define OUT(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    exit(0);                                                                   \
  }
#define REYNA(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define scanf(...) scanf(__VA_ARGS__) ?: 0
typedef long long int ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef pair<PLL, int> PLLI;
typedef pair<PLL, pair<int, ll>> PP;
typedef pair<PII, int> PPI;
typedef pair<int, PII> PIP;
typedef pair<ll, int> PLI;
typedef unsigned int ui;
const int inf = 1e9 + 9;
const ll mod = 1e9 + 696969;
const ll MOD = 1e9 + 696969;
const long long INF = 1e18 + 3;
const int maxn = 310;
bool a[maxn][maxn];

bool canBeGenerated[maxn];
bool atLeastOneBlack;
int n;

int main() {
  boost;
  cin >> n;
  FOR(i, 1, n)
  FOR(j, 1, n) {
    char zn;
    cin >> zn;
    a[i][j] = (zn == '#' ? 0 : 1);
    if (!a[i][j]) {
      atLeastOneBlack = true;
      canBeGenerated[j] = true;
    }
  }

  int ROW = 2 * n + 5;
  FOR(i, 1, n) {
    int rowCost = 0;
    FOR(j, 1, n) rowCost += a[i][j];
    if (!canBeGenerated[i])
      ++rowCost;
    ROW = min(ROW, rowCost);
  }

  if (!atLeastOneBlack)
    OUT(-1);
  int second_cost = 0;
  FOR(j, 1, n) {
    bool add = false;
    FOR(i, 1, n) add |= a[i][j];
    second_cost += add;
  }

  cout << ROW + second_cost;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define pb push_back
#define mp make_pair
#define boost ios_base::sync_with_stdio(false)
#define eb emplace_back
#define OUT(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    exit(0);                                                                   \
  }
#define REYNA(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define scanf(...) scanf(__VA_ARGS__) ?: 0
typedef long long int ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef pair<PLL, int> PLLI;
typedef pair<PLL, pair<int, ll>> PP;
typedef pair<PII, int> PPI;
typedef pair<int, PII> PIP;
typedef pair<ll, int> PLI;
typedef unsigned int ui;
const int inf = 1e9 + 9;
const ll mod = 1e9 + 696969;
const ll MOD = 1e9 + 696969;
const long long INF = 1e18 + 3;
const int maxn = 510;
bool a[maxn][maxn];

bool canBeGenerated[maxn];
bool atLeastOneBlack;
int n;

int main() {
  boost;
  cin >> n;
  FOR(i, 1, n)
  FOR(j, 1, n) {
    char zn;
    cin >> zn;
    a[i][j] = (zn == '#' ? 0 : 1);
    if (!a[i][j]) {
      atLeastOneBlack = true;
      canBeGenerated[j] = true;
    }
  }

  int ROW = 2 * n + 5;
  FOR(i, 1, n) {
    int rowCost = 0;
    FOR(j, 1, n) rowCost += a[i][j];
    if (!canBeGenerated[i])
      ++rowCost;
    ROW = min(ROW, rowCost);
  }

  if (!atLeastOneBlack)
    OUT(-1);
  int second_cost = 0;
  FOR(j, 1, n) {
    bool add = false;
    FOR(i, 1, n) add |= a[i][j];
    second_cost += add;
  }

  cout << ROW + second_cost;
}