Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

void printA(const vector<vector<char>> &a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j];
    }
    cout << endl;
  }
}

void printAns(const vector<vector<int>> &a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
}

int main() {
  int h, w;
  cin >> h >> w;

  vector<vector<char>> a(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }

  vector<vector<int>> ans(w, vector<int>(h, INT_MAX));

  // for right
  for (int i = 0; i < h; i++) {
    bool hitBlack = false;
    for (int j = 0; j < w; j++) {
      if (a[i][j] == '#') {
        hitBlack = true;
        ans[i][j] = 0;
      } else if (hitBlack) {
        chmin(ans[i][j], ans[i][j - 1] + 1);
      }
    }
  }

  // for left
  for (int i = 0; i < h; i++) {
    bool hitBlack = false;
    for (int j = w - 1; j >= 0; j--) {
      if ((!hitBlack) && (ans[i][j] != INT_MAX)) {
        hitBlack = true;
        continue;
      }

      if (hitBlack) {
        chmin(ans[i][j], ans[i][j + 1] + 1);
      }
    }
  }

  // for bottom
  for (int j = 0; j < w; j++) {
    bool hitBlack = false;
    for (int i = 0; i < h; i++) {
      if ((!hitBlack) && (ans[i][j] != INT_MAX)) {
        hitBlack = true;
        continue;
      }

      if (hitBlack) {
        chmin(ans[i][j], ans[i - 1][j] + 1);
      }
    }
  }

  // for top
  for (int j = 0; j < w; j++) {
    bool hitBlack = false;
    for (int i = h - 1; i >= 0; i--) {
      if ((!hitBlack) && (ans[i][j] != INT_MAX)) {
        hitBlack = true;
        continue;
      }

      if (hitBlack) {
        chmin(ans[i][j], ans[i + 1][j] + 1);
      }
    }
  }

  // printAns(ans);

  int maxVal = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      chmax(maxVal, ans[i][j]);
    }
  }

  cout << maxVal << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

void printA(const vector<vector<char>> &a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j];
    }
    cout << endl;
  }
}

void printAns(const vector<vector<int>> &a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) {
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
}

int main() {
  int h, w;
  cin >> h >> w;

  vector<vector<char>> a(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }

  vector<vector<int>> ans(h, vector<int>(w, INT_MAX));

  // for right
  for (int i = 0; i < h; i++) {
    bool hitBlack = false;
    for (int j = 0; j < w; j++) {
      if (a[i][j] == '#') {
        hitBlack = true;
        ans[i][j] = 0;
      } else if (hitBlack) {
        chmin(ans[i][j], ans[i][j - 1] + 1);
      }
    }
  }

  // for left
  for (int i = 0; i < h; i++) {
    bool hitBlack = false;
    for (int j = w - 1; j >= 0; j--) {
      if ((!hitBlack) && (ans[i][j] != INT_MAX)) {
        hitBlack = true;
        continue;
      }

      if (hitBlack) {
        chmin(ans[i][j], ans[i][j + 1] + 1);
      }
    }
  }

  // for bottom
  for (int j = 0; j < w; j++) {
    bool hitBlack = false;
    for (int i = 0; i < h; i++) {
      if ((!hitBlack) && (ans[i][j] != INT_MAX)) {
        hitBlack = true;
        continue;
      }

      if (hitBlack) {
        chmin(ans[i][j], ans[i - 1][j] + 1);
      }
    }
  }

  // for top
  for (int j = 0; j < w; j++) {
    bool hitBlack = false;
    for (int i = h - 1; i >= 0; i--) {
      if ((!hitBlack) && (ans[i][j] != INT_MAX)) {
        hitBlack = true;
        continue;
      }

      if (hitBlack) {
        chmin(ans[i][j], ans[i + 1][j] + 1);
      }
    }
  }

  // printAns(ans);

  int maxVal = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      chmax(maxVal, ans[i][j]);
    }
  }

  cout << maxVal << endl;
  return 0;
}