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

#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

string S[2000];
int x[2000][2000];
int z[2000][2000];
queue<int> Q;
queue<int> R;

int main() {
  int n, m;
  char T[10] = {'.', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < 10; k++) {
        if (S[i][j] == T[k]) {
          x[i][j] = k;
        }
      }
    }
  }
  int t = 1;
  int p = 0;
  int sum = 0;
  memset(z, 0, sizeof(z));
  t = 0;
  sum++;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (x[i][j] >= 1) {
        p = 0;
        for (int k = -1; k <= 1; k++) {
          for (int l = -1; l <= 1; l++) {
            if (x[i + k][j + l] == 0) {
              p++;
            }
          }
        }
        if (p >= x[i][j]) {
          z[i][j] = 1;
          Q.push(i * 1000 + j);
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (z[i][j] == 1) {
        x[i][j] = 0;
      }
    }
  }

  int c, d;

  while (sum == 0 || !Q.empty()) {
    sum++;
    while (!Q.empty()) {
      c = Q.front() / 1000;
      d = Q.front() % 1000;
      for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
          if (x[c + i][d + j] >= 1) {
            p = 0;
            for (int k = -1; k <= 1; k++) {
              for (int l = -1; l <= 1; l++) {
                if (x[c + i + k][d + j + l] == 0) {
                  p++;
                }
              }
            }
            if (p >= x[c + i][d + j] && x[c + i][d + j] != 10) {
              x[c + i][d + j] = 10;
              R.push((c + i) * 1000 + (d + j));
            }
          }
        }
      }
      Q.pop();
    }
    while (!R.empty()) {
      c = R.front() / 1000;
      d = R.front() % 1000;
      x[c][d] = 0;
      Q.push(R.front());
      R.pop();
    }
  }

  cout << sum - 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>
using namespace std;

string S[2000];
int x[2000][2000];
int z[2000][2000];

int main() {
  int n, m;
  char T[10] = {'.', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < 10; k++) {
        if (S[i][j] == T[k]) {
          x[i][j] = k;
        }
      }
    }
  }
  int t = 1;
  int p = 0;
  int sum = 0;
  while (t != 0) {
    memset(z, 0, sizeof(z));
    t = 0;
    sum++;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (x[i][j] >= 1) {
          p = 0;
          for (int k = -1; k <= 1; k++) {
            for (int l = -1; l <= 1; l++) {
              if (x[i + k][j + l] == 0) {
                p++;
              }
            }
          }
          if (p >= x[i][j]) {
            z[i][j] = 1;
            t++;
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (z[i][j] == 1) {
          x[i][j] = 0;
        }
      }
    }
  }
  cout << sum - 1 << endl;
  return 0;
}