Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

vector<vector<int>> ichigo;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  ichigo.resize(H);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      if (c == '#')
        ichigo[i].push_back(j);
    }
  }

  int nothing = -1;
  int ans[300][300] = {};
  int cnt = 1;
  for (int i = 0; i < H; i++) {
    if (ichigo[i].size() == 0 && nothing == -1) {
      nothing = i;
    } else {
      int s;
      if (nothing == -1)
        s = i;
      else
        s = nothing;
      nothing = -1;
      if (ichigo[i].size() == 1) {
        for (int w = 0; w < W; w++) {
          for (int h = s; h <= i; h++)
            ans[h][w] = cnt;
        }
      } else {
        for (int w = 0; w <= ichigo[i][0]; w++)
          for (int h = s; h <= i; h++)
            ans[h][w] = cnt;
        int idx = 1;
        for (int w = ichigo[i][0] + 1; w < W; w++) {
          for (int h = s; h <= i; h++)
            ans[h][w] = cnt;
          if (idx < ichigo[i].size() && ichigo[i][idx] == w) {
            cnt++;
            idx++;
            for (int h = s; h <= i; h++)
              ans[h][w] = cnt;
          }
        }
      }
      cnt++;
    }
  }
  if (nothing != -1) {
    cnt -= ichigo[nothing - 1].size();
    int s = nothing;
    if (ichigo[nothing - 1].size() == 1) {
      for (int w = 0; w < W; w++) {
        for (int h = s; h < H; h++)
          ans[h][w] = cnt;
      }
    } else {
      for (int w = 0; w <= ichigo[nothing - 1][0]; w++)
        for (int h = s; h < H; h++)
          ans[h][w] = cnt;
      int idx = 1;
      for (int w = ichigo[nothing - 1][0] + 1; w < W; w++) {
        for (int h = s; h < H; h++)
          ans[h][w] = cnt;
        if (idx < ichigo[nothing - 1].size() && ichigo[nothing - 1][idx] == w) {
          cnt++;
          idx++;
          for (int h = s; h < H; h++)
            ans[h][w] = cnt;
        }
      }
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      cout << ans[i][j] << " ";
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

vector<vector<int>> ichigo;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  ichigo.resize(H);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      if (c == '#')
        ichigo[i].push_back(j);
    }
  }

  int nothing = -1;
  int ans[300][300] = {};
  int cnt = 1;
  for (int i = 0; i < H; i++) {
    if (ichigo[i].size() == 0 && nothing == -1) {
      nothing = i;
    } else if (ichigo[i].size() != 0) {
      int s;
      if (nothing == -1)
        s = i;
      else
        s = nothing;
      nothing = -1;
      if (ichigo[i].size() == 1) {
        for (int w = 0; w < W; w++) {
          for (int h = s; h <= i; h++)
            ans[h][w] = cnt;
        }
      } else {
        for (int w = 0; w <= ichigo[i][0]; w++)
          for (int h = s; h <= i; h++)
            ans[h][w] = cnt;
        int idx = 1;
        for (int w = ichigo[i][0] + 1; w < W; w++) {
          for (int h = s; h <= i; h++)
            ans[h][w] = cnt;
          if (idx < ichigo[i].size() && ichigo[i][idx] == w) {
            cnt++;
            idx++;
            for (int h = s; h <= i; h++)
              ans[h][w] = cnt;
          }
        }
      }
      cnt++;
    }
  }
  if (nothing != -1) {
    cnt -= ichigo[nothing - 1].size();
    int s = nothing;
    if (ichigo[nothing - 1].size() == 1) {
      for (int w = 0; w < W; w++) {
        for (int h = s; h < H; h++)
          ans[h][w] = cnt;
      }
    } else {
      for (int w = 0; w <= ichigo[nothing - 1][0]; w++)
        for (int h = s; h < H; h++)
          ans[h][w] = cnt;
      int idx = 1;
      for (int w = ichigo[nothing - 1][0] + 1; w < W; w++) {
        for (int h = s; h < H; h++)
          ans[h][w] = cnt;
        if (idx < ichigo[nothing - 1].size() && ichigo[nothing - 1][idx] == w) {
          cnt++;
          idx++;
          for (int h = s; h < H; h++)
            ans[h][w] = cnt;
        }
      }
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      cout << ans[i][j] << " ";
    cout << endl;
  }
  return 0;
}