Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
constexpr int64_t mod = 1e9 + 7;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (auto &&e : s) {
    cin >> e;
  }
  vector<vector<int>> number(h, vector<int>(w, 0));
  int cnt = 1;

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      number[i][j] = cnt;
      if (s[i][j] == '#')
        cnt++;
    }
    if (s[i][w - 1] == '.') {
      for (int j = 0; j < w; ++j) {
        if (number[i][j] == cnt)
          number[i][j]--;
      }
    }
    cnt = number[i][w - 1] + 1;
  }

  vector<int> no_hash;
  for (int i = 0; i < h; ++i) {
    bool flag = true;
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == '#')
        flag = false;
    }
    if (flag) {
      no_hash.push_back(i);
    }
  }
  if (*no_hash.rbegin() == h - 1) {
    int tmp = h - 1;
    while (tmp != 0) {
      tmp--;
      if (!binary_search(no_hash.begin(), no_hash.end(), tmp)) {
        break;
      }
    }
    while (tmp != h - 1) {
      tmp++;
      for (int j = 0; j < w; ++j) {
        number[tmp][j] = number[tmp - 1][j];
      }
    }
  }

  for (int i = 0; i < no_hash.size(); ++i) {
    int tmp = no_hash[i];
    while (tmp != h - 1) {
      tmp++;
      if (!binary_search(no_hash.begin(), no_hash.end(), tmp)) {
        break;
      }
    }
    while (tmp != no_hash[i]) {
      tmp--;
      for (int j = 0; j < w; ++j) {
        number[tmp][j] = number[tmp + 1][j];
      }
    }
  }

  // output
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cout << number[i][j] << " ";
    }
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
constexpr int64_t mod = 1e9 + 7;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (auto &&e : s) {
    cin >> e;
  }
  vector<vector<int>> number(h, vector<int>(w, 0));
  int cnt = 1;

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      number[i][j] = cnt;
      if (s[i][j] == '#')
        cnt++;
    }
    if (s[i][w - 1] == '.') {
      for (int j = 0; j < w; ++j) {
        if (number[i][j] == cnt)
          number[i][j]--;
      }
    }
    cnt = number[i][w - 1] + 1;
  }

  vector<int> no_hash;
  for (int i = 0; i < h; ++i) {
    bool flag = true;
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == '#')
        flag = false;
    }
    if (flag) {
      no_hash.push_back(i);
    }
  }
  if (!no_hash.empty() && *no_hash.rbegin() == h - 1) {
    int tmp = h - 1;
    while (tmp != 0) {
      tmp--;
      if (!binary_search(no_hash.begin(), no_hash.end(), tmp)) {
        break;
      }
    }
    while (tmp != h - 1) {
      tmp++;
      for (int j = 0; j < w; ++j) {
        number[tmp][j] = number[tmp - 1][j];
      }
    }
  }

  for (int i = 0; i < no_hash.size(); ++i) {
    int tmp = no_hash[i];
    while (tmp != h - 1) {
      tmp++;
      if (!binary_search(no_hash.begin(), no_hash.end(), tmp)) {
        break;
      }
    }
    while (tmp != no_hash[i]) {
      tmp--;
      for (int j = 0; j < w; ++j) {
        number[tmp][j] = number[tmp + 1][j];
      }
    }
  }

  // output
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cout << number[i][j] << " ";
    }
    cout << endl;
  }
}