Problem p02855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
#define INF (1LL << 62)

#define PI (acos(-1))
#define print(x) cout << x << endl

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
typedef pair<ll, ll> P;
static const ll dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); ++i) {
    os << v[i];
    if (i != (int)v.size() - 1)
      os << " ";
  }
  return os;
}

int main() {
  ll H, W, K;
  cin >> H >> W >> K;
  vector<vector<ll>> s(H, vector<ll>(W));
  vector<vector<ll>> a(H, vector<ll>(W, 0));
  rep(i, H) {
    string t;
    cin >> t;
    rep(j, W) { s[i][j] = t[j] == '#'; }
  }
  bool exist = false;
  ll num = 1;
  rep(i, H) {
    exist = false;
    ll pre = 0;
    rep(j, W) {
      if (s[i][j]) {
        exist = true;
        for (ll k = pre; k <= j; k++) {
          a[i][k] = num;
        }
        pre = j + 1;
        num++;
      } else if (exist && j == W - 1) {
        for (ll k = pre; k <= j; k++) {
          a[i][k] = num - 1;
        }
      }
    }
  }
  bool flag = true;
  while (flag) {
    flag = false;
    rep(i, H) {
      if (a[i][0] == 0) {
        flag = true;
        if (i == 0) {
          rep(j, W) { a[i][j] = a[i + 1][j]; }
        } else {
          rep(j, W) { a[i][j] = a[i - 1][j]; }
        }
      }
    }
  }

  rep(i, H) print(a[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
#define INF (1LL << 62)

#define PI (acos(-1))
#define print(x) cout << x << endl

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
typedef pair<ll, ll> P;
static const ll dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); ++i) {
    os << v[i];
    if (i != (int)v.size() - 1)
      os << " ";
  }
  return os;
}

int main() {
  ll H, W, K;
  cin >> H >> W >> K;
  vector<vector<ll>> s(H, vector<ll>(W));
  vector<vector<ll>> a(H, vector<ll>(W, 0));
  rep(i, H) {
    string t;
    cin >> t;
    rep(j, W) { s[i][j] = t[j] == '#'; }
  }
  bool exist = false;
  ll num = 1;
  rep(i, H) {
    exist = false;
    ll pre = 0;
    rep(j, W) {
      if (s[i][j]) {
        exist = true;
        for (ll k = pre; k <= j; k++) {
          a[i][k] = num;
        }
        pre = j + 1;
        num++;
      } else if (exist && j == W - 1) {
        for (ll k = pre; k <= j; k++) {
          a[i][k] = num - 1;
        }
      }
    }
  }
  rep(i, H) {
    if (i == 0)
      continue;
    if (a[i][0] == 0) {
      rep(j, W) { a[i][j] = a[i - 1][j]; }
    }
  }

  repr(i, H) {
    if (i == H - 1)
      continue;
    if (a[i][0] == 0) {
      rep(j, W) { a[i][j] = a[i + 1][j]; }
    }
  }

  rep(i, H) print(a[i]);
  return 0;
}