Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fr(i, n) for (int i = 0; i < n; i++)
#define ifr(i, n) for (int i = n - 1; i >= 0; i--)

int main() {
  int h, w;
  int x = 0;
  cin >> h >> w;
  string kari[h];
  int a[h + 2][w + 2];
  fr(i, h + 2) {
    fr(j, w + 2) { a[i][j] = 10000; }
  }
  bool flag = true;
  fr(i, h) { cin >> kari[i]; }
  fr(i, h) {
    fr(j, w) {
      if (kari[i].substr(j, 1) == "#")
        a[i + 1][j + 1] = 0;
      if (kari[i].substr(j, 1) == ".")
        a[i + 1][j + 1] = 10000;
    }
  }
  for (x = 0; x < w + h; x++) {
    flag = true;
    fr(i, h) {
      fr(j, w) {
        if (a[i + 1][j + 1] > a[i][j + 1] + 1) {
          a[i + 1][j + 1] = a[i][j + 1] + 1;
          flag = false;
        }
        if (a[i + 1][j + 1] > a[i + 2][j + 1] + 1) {
          a[i + 1][j + 1] = a[i + 2][j + 1] + 1;
          flag = false;
        }
        if (a[i + 1][j + 1] > a[i + 1][j] + 1) {
          a[i + 1][j + 1] = a[i + 1][j] + 1;
          flag = false;
        }
        if (a[i + 1][j + 1] > a[i + 1][j + 2] + 1) {
          a[i + 1][j + 1] = a[i + 1][j + 2] + 1;
          flag = false;
        }
      }
    }
    if (flag == true)
      break;
  }
  int max = 0;
  fr(i, h) {
    fr(j, w) {
      if (a[i + 1][j + 1] > max)
        max = a[i + 1][j + 1];
    }
  }
  cout << max << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fr(i, n) for (int i = 0; i < n; i++)
#define ifr(i, n) for (int i = n - 1; i >= 0; i--)

int main() {
  int h, w;
  int x = 0;
  cin >> h >> w;
  string kari[h];
  int a[h + 2][w + 2];
  fr(i, h + 2) {
    fr(j, w + 2) { a[i][j] = 10000; }
  }
  bool flag = true;
  fr(i, h) { cin >> kari[i]; }
  fr(i, h) {
    fr(j, w) {
      if (kari[i].substr(j, 1) == "#")
        a[i + 1][j + 1] = 0;
      if (kari[i].substr(j, 1) == ".")
        a[i + 1][j + 1] = 10000;
    }
  }
  for (x = 0; x < w + h; x++) {
    flag = true;
    fr(i, h) {
      fr(j, w) {
        if (a[i + 1][j + 1] > a[i][j + 1] + 1) {
          a[i + 1][j + 1] = a[i][j + 1] + 1;
          flag = false;
        }
        if (a[i + 1][j + 1] > a[i + 2][j + 1] + 1) {
          a[i + 1][j + 1] = a[i + 2][j + 1] + 1;
          flag = false;
        }
        if (a[i + 1][j + 1] > a[i + 1][j] + 1) {
          a[i + 1][j + 1] = a[i + 1][j] + 1;
          flag = false;
        }
        if (a[i + 1][j + 1] > a[i + 1][j + 2] + 1) {
          a[i + 1][j + 1] = a[i + 1][j + 2] + 1;
          flag = false;
        }
      }
    }
    if (flag == true)
      break;
    ifr(i, h) {
      ifr(j, w) {
        if (a[i + 1][j + 1] > a[i][j + 1] + 1) {
          a[i + 1][j + 1] = a[i][j + 1] + 1;
          flag = false;
        }
        if (a[i + 1][j + 1] > a[i + 2][j + 1] + 1) {
          a[i + 1][j + 1] = a[i + 2][j + 1] + 1;
          flag = false;
        }
        if (a[i + 1][j + 1] > a[i + 1][j] + 1) {
          a[i + 1][j + 1] = a[i + 1][j] + 1;
          flag = false;
        }
        if (a[i + 1][j + 1] > a[i + 1][j + 2] + 1) {
          a[i + 1][j + 1] = a[i + 1][j + 2] + 1;
          flag = false;
        }
      }
    }
    if (flag == true)
      break;
  }
  int max = 0;
  fr(i, h) {
    fr(j, w) {
      if (a[i + 1][j + 1] > max)
        max = a[i + 1][j + 1];
    }
  }
  cout << max << endl;
}