Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back

using namespace std;
using ll = long long;
using ii = pair<int, int>;

const int N = 305;

int ans[N][N];
int main() {
  int h, w, _;
  char mat[N][N];
  scanf("%d %d %d", &h, &w, &_);
  for (int i = 0; i < h; i++) {
    scanf("%s", mat[i]);
  }
  int id = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (mat[i][j] == '#') {
        int up = i, down = i;
        ans[i][j] = id;
        while (up > 0 && mat[up - 1][j] == '.' && ans[up - 1][j] == 0) {
          up--;
          ans[up][j] = id;
        }
        while (down < h - 1 && mat[down + 1][j] == '.' &&
               ans[down + 1][j] == 0) {
          down++;
          ans[down][j] = id;
        }
        for (int k = j - 1; k >= 0; k--) {
          int up2 = i, down2 = i;
          if (ans[i][k] != 0 || mat[i][k] != '.')
            break;
          while (up2 > up && mat[up2 - 1][k] == '.' && ans[up2 - 1][k] == 0) {
            up2--;
          }
          while (down2 < down && mat[down2 + 1][k] == '.' &&
                 ans[down2 + 1][k] == 0) {
            down2++;
          }
          if (up2 != up || down2 != down) {
            break;
          }
          for (int l = up2; l <= down2; l++) {
            ans[l][k] = id;
          }
        }
        for (int k = j + 1; k < w; k++) {
          int up2 = i, down2 = i;
          if (ans[i][k] != 0 || mat[i][k] != '.')
            break;
          while (up2 > up && mat[up2 - 1][k] == '.' && ans[up2 - 1][k] == 0) {
            up2--;
          }
          while (down2 < down && mat[down2 + 1][k] == '.' &&
                 ans[down2 + 1][k] == 0) {
            down2++;
          }
          if (up2 != up || down2 != down) {
            break;
          }
          for (int l = up2; l <= down2; l++) {
            ans[l][k] = id;
          }
        }
        id++;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      assert(ans[i][j] != 0);
      printf("%d%c", ans[i][j], j == w - 1 ? '\n' : ' ');
    }
  }
}


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

#define ff first
#define ss second
#define pb push_back

using namespace std;
using ll = long long;
using ii = pair<int, int>;

const int N = 305;

int ans[N][N];
int main() {
  int h, w, _;
  char mat[N][N];
  scanf("%d %d %d", &h, &w, &_);
  for (int i = 0; i < h; i++) {
    scanf("%s", mat[i]);
  }
  int id = 1;
  for (int j = 0; j < w; j++) {
    for (int i = 0; i < h; i++) {
      if (mat[i][j] == '#') {
        int up = i, down = i;
        ans[i][j] = id;
        while (up > 0 && mat[up - 1][j] == '.' && ans[up - 1][j] == 0) {
          up--;
          ans[up][j] = id;
        }
        while (down < h - 1 && mat[down + 1][j] == '.' &&
               ans[down + 1][j] == 0) {
          down++;
          ans[down][j] = id;
        }
        for (int k = j - 1; k >= 0; k--) {
          int up2 = i, down2 = i;
          if (ans[i][k] != 0 || mat[i][k] != '.')
            break;
          while (up2 > up && mat[up2 - 1][k] == '.' && ans[up2 - 1][k] == 0) {
            up2--;
          }
          while (down2 < down && mat[down2 + 1][k] == '.' &&
                 ans[down2 + 1][k] == 0) {
            down2++;
          }
          if (up2 != up || down2 != down) {
            break;
          }
          for (int l = up2; l <= down2; l++) {
            ans[l][k] = id;
          }
        }
        for (int k = j + 1; k < w; k++) {
          int up2 = i, down2 = i;
          if (ans[i][k] != 0 || mat[i][k] != '.')
            break;
          while (up2 > up && mat[up2 - 1][k] == '.' && ans[up2 - 1][k] == 0) {
            up2--;
          }
          while (down2 < down && mat[down2 + 1][k] == '.' &&
                 ans[down2 + 1][k] == 0) {
            down2++;
          }
          if (up2 != up || down2 != down) {
            break;
          }
          for (int l = up2; l <= down2; l++) {
            ans[l][k] = id;
          }
        }
        id++;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      assert(ans[i][j] != 0);
      printf("%d%c", ans[i][j], j == w - 1 ? '\n' : ' ');
    }
  }
}