Problem p00535 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
struct P {
  int time, cost;
};
struct Q {
  int x, y;
};
const int inf = 1000000;
int a[1000][1000];
int g[1000][1000];
P b[1000][1000];
int main() {
  int h, w;
  cin >> h >> w;
  string s;
  for (int i = 0; i < h; i++) {
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == '.')
        a[i][j] = 0;
      else
        a[i][j] = (int)s[j] - '0';
      g[i][j] = 0;
      b[i][j] = P{0, 0};
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 0) {
        for (int x = -1; x <= 1; x++) {
          for (int y = -1; y <= 1; y++) {
            if (i + x < h && i + x >= 0 && j + y < w && j + y >= 0) {
              g[i + x][j + y]++;
            }
          }
        }
      }
    }
  }
  queue<Q> p;
  int sum = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] != 0 && a[i][j] <= g[i][j]) {
        a[i][j] = 0;
        b[i][j] = P{1, 0};
        sum = 1;
        for (int x = -1; x <= 1; x++) {
          for (int y = -1; y <= 1; y++) {
            if (i + x < h && i + x >= 0 && j + y < w && j + y >= 0) {
              p.push(Q{i + x, j + y});
              b[i + x][j + y] = P{2, b[i + x][j + y].cost + 1};
            }
          }
        }
      }
    }
  }
  while (!p.empty()) {
    Q t = p.front();
    p.pop();
    int i = t.x, j = t.y;
    if (a[i][j] <= g[i][j] + b[i][j].cost && a[i][j] != 0) {
      a[i][j] = 0;
      sum = max(sum, b[i][j].time);
      for (int x = -1; x <= 1; x++) {
        for (int y = -1; y <= 1; y++) {
          if (i + x < h && i + x >= 0 && j + y < w && j + y >= 0 &&
              a[i + x][j + y] != 0) {
            if (a[i + x][j + y] > g[i + x][j + y] + b[i + x][j + y].cost) {
              b[i + x][j + y] = P{max(b[i + x][j + y].time, b[i][j].time + 1),
                                  b[i + x][j + y].cost + 1};
              if (b[i + x][j + y].time >= 1736) {
                int o = 0;
              }
            }
            if (a[i + x][j + y] != 0 &&
                a[i + x][j + y] <= g[i + x][j + y] + b[i + x][j + y].cost) {
              p.push(Q{i + x, j + y});
            }
          }
        }
      }
    }
  }
  cout << sum << endl;
  cin >> sum;
}

Error: TLE

Fixed submission:

#include <bits //stdc++.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
struct P {
  int x, y;
};
int main() {
  int a[1000][1000], g[1000][1000];
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == '.')
        a[i][j] = 0;
      else
        a[i][j] = (int)s[j] - '0';
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      g[i][j] = 0;
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 0) {
        for (int x = -1; x <= 1; x++) {
          for (int y = -1; y <= 1; y++) {
            if (i + x < h && i + x >= 0 && j + y < w && j + y >= 0) {
              g[i + x][j + y]++;
            }
          }
        }
      }
    }
  }
  int sum = 0;
  bool l = true;
  while (l) {
    l = false;
    vector<P> p;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (g[i][j] >= a[i][j] && a[i][j] != 0) {
          a[i][j] = 0;
          p.push_back(P{i, j});
          l = true;
        }
      }
    }
    for (int i = 0; i < (int)p.size(); i++) {
      for (int x = -1; x <= 1; x++) {
        for (int y = -1; y <= 1; y++) {
          g[p[i].x + x][p[i].y + y]++;
        }
      }
    }
    sum++;
  }
  cout << --sum << endl;
}