Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <new>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;
constexpr ll mop = 1000000007;
constexpr ll mop2 = 998244353;

using namespace std;

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  vector<vector<bool>> grid(h);
  vector<bool> existSt(h);
  vector<vector<ll>> ans(h);
  ll minrow = INT64_MAX;

  for (ll i = 0; i < h; i++) {
    grid[i].resize(w);
    ans[i].resize(w);
    string str;
    cin >> str;
    for (ll j = 0; j < w; j++) {
      if (str[j] == '#') {
        grid[i][j] = true;
        if (minrow > j) {
          minrow = j;
        }
        existSt[i] = true;
      }
    }
  }
  ll nowNo = 1;
  for (ll i = minrow; i < h; i++) {
    if (existSt[i] == false) {
      for (ll j = 0; j < w; j++) {
        ans[i][j] = ans[i - 1][j];
      }
    } else {
      bool discoverOne = false;
      for (ll j = 0; j < w; j++) {
        if (grid[i][j] == true) {
          if (discoverOne) {
            nowNo++;
          } else {
            discoverOne = true;
          }
        }
        ans[i][j] = nowNo;
      }
      nowNo++;
    }
  }
  for (ll i = 0; i < minrow; i++) {
    for (ll j = 0; j < w; j++) {
      if (j != 0) {
        cout << " ";
      }
      cout << ans[minrow][j];
    }
    cout << endl;
  }
  for (ll i = minrow; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      if (j != 0) {
        cout << " ";
      }
      cout << ans[i][j];
    }
    cout << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <new>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;
constexpr ll mop = 1000000007;
constexpr ll mop2 = 998244353;

using namespace std;

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  vector<vector<bool>> grid(h);
  vector<bool> existSt(h);
  vector<vector<ll>> ans(h);
  ll minrow = INT64_MAX;

  for (ll i = 0; i < h; i++) {
    grid[i].resize(w);
    ans[i].resize(w);
    string str;
    cin >> str;
    for (ll j = 0; j < w; j++) {
      if (str[j] == '#') {
        grid[i][j] = true;
        if (minrow > i) {
          minrow = i;
        }
        existSt[i] = true;
      }
    }
  }
  ll nowNo = 1;
  for (ll i = minrow; i < h; i++) {
    if (existSt[i] == false) {
      for (ll j = 0; j < w; j++) {
        ans[i][j] = ans[i - 1][j];
      }
    } else {
      bool discoverOne = false;
      for (ll j = 0; j < w; j++) {
        if (grid[i][j] == true) {
          if (discoverOne) {
            nowNo++;
          } else {
            discoverOne = true;
          }
        }
        ans[i][j] = nowNo;
      }
      nowNo++;
    }
  }
  for (ll i = 0; i < minrow; i++) {
    for (ll j = 0; j < w; j++) {
      if (j != 0) {
        cout << " ";
      }
      cout << ans[minrow][j];
    }
    cout << endl;
  }
  for (ll i = minrow; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      if (j != 0) {
        cout << " ";
      }
      cout << ans[i][j];
    }
    cout << endl;
  }
}