Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#pragma region header
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for (int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
constexpr ll mod = 1e9 + 7;
constexpr int inf = INT32_MAX / 2;
constexpr ll INF = LLONG_MAX / 2;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */

/* --------------------テンプレート------------------ */
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/* ----------------------end----------------------- */

/* --------------------ライブラリ-------------------- */
ll fact(int i) { // 階乗
  if (i == 0)
    return 1;
  return (fact(i - 1)) * i % mod;
}
ll gcd(ll a, ll b) { // 最大公約数
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { // 最小公倍数
  return a * b / gcd(a, b);
}
int keta(ll n) { // 桁数を求める
  if (n == 0)
    return 1;
  int count = 0;
  while (n != 0) {
    n /= 10;
    count++;
  }
  return count;
}
ll ketasum(ll n) { // 各桁の和
  ll sum = 0;
  while (n != 0) {
    sum += n % 10;
    n /= 10;
  }
  return sum;
}
/* ----------------------end----------------------- */
#pragma endregion
signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  vvi ans(h, vi(w));
  int a = 1;
  rep(i, h) {
    rep(j, w) {
      char c;
      cin >> c;
      if (c == '#') {
        ans[i][j] = a;
        a++;
      } else if (j != 0)
        ans[i][j] = ans[i][j - 1];
    }
  }
  rep(i, h) if (ans[i][0] == 0) ans[i][0] = ans[i][1];
  rep(j, w) {
    rep(i, h) {
      if (ans[i][j] == 0) {
        if (i == 0)
          ans[i][j] = ans[i + 1][j];
        else
          ans[i][j] = ans[i - 1][j];
      }
    }
  }
  rep(i, h) {
    rep(j, w) {
      cout << ans[i][j];
      if (j == w - 1)
        cout << endl;
      else
        cout << ' ';
    }
  }
  return 0;
}


Fixed submission:
#pragma region header
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for (int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
constexpr ll mod = 1e9 + 7;
constexpr int inf = INT32_MAX / 2;
constexpr ll INF = LLONG_MAX / 2;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */

/* --------------------テンプレート------------------ */
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/* ----------------------end----------------------- */

/* --------------------ライブラリ-------------------- */
ll fact(int i) { // 階乗
  if (i == 0)
    return 1;
  return (fact(i - 1)) * i % mod;
}
ll gcd(ll a, ll b) { // 最大公約数
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { // 最小公倍数
  return a * b / gcd(a, b);
}
int keta(ll n) { // 桁数を求める
  if (n == 0)
    return 1;
  int count = 0;
  while (n != 0) {
    n /= 10;
    count++;
  }
  return count;
}
ll ketasum(ll n) { // 各桁の和
  ll sum = 0;
  while (n != 0) {
    sum += n % 10;
    n /= 10;
  }
  return sum;
}
/* ----------------------end----------------------- */
#pragma endregion
signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  vvi ans(h, vi(w));
  int a = 1;
  rep(i, h) {
    rep(j, w) {
      char c;
      cin >> c;
      if (c == '#') {
        ans[i][j] = a;
        a++;
      } else if (j != 0)
        ans[i][j] = ans[i][j - 1];
    }
  }
  rep(i, h) {
    rev(j, w - 1) {
      if (ans[i][j] == 0)
        ans[i][j] = ans[i][j + 1];
    }
  }
  rep(x, 300) {
    rep(j, w) {
      rep(i, h) {
        if (ans[i][j] == 0) {
          if (i == 0)
            ans[i][j] = ans[i + 1][j];
          else
            ans[i][j] = ans[i - 1][j];
        }
      }
    }
  }
  rep(x, 300) {
    rep(j, w) {
      rev(i, h) {
        if (ans[i][j] == 0) {
          if (i == h - 1)
            ans[i][j] = ans[i - 1][j];
          else
            ans[i][j] = ans[i + 1][j];
        }
      }
    }
  }
  rep(i, h) {
    rep(j, w) {
      cout << ans[i][j];
      if (j == w - 1)
        cout << endl;
      else
        cout << ' ';
    }
  }
  return 0;
}