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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
string fie[1001];
int fiec[1001][1001];
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct data {
  int y;
  int x;
  int n;
};
queue<data> Q;
queue<int> lisy;
queue<int> lisx;
int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; i++)
    cin >> fie[i];
  for (int i = 1; i < H - 1; i++) {
    for (int j = 1; j < W - 1; j++) {
      int cou = 0;
      for (int k = 0; k < 8; k++) {
        if (fie[i + dy[k]][j + dx[k]] == '.')
          cou++;
      }
      if (fie[i][j] - '0' <= cou && fie[i][j] != '.')
        lisy.push(i), lisx.push(j);
    }
  }
  int befn = 0;
  while (1) {
    if (lisy.size() == 0)
      break;
    befn++;
    if (Q.size() == 0) {
      while (lisy.size()) {
        int kariy, karix;
        kariy = lisy.front();
        karix = lisx.front(), lisy.pop(), lisx.pop();
        fie[kariy][karix] = '.';
        for (int k = 0; k < 8; k++) {
          if (fiec[kariy + dy[k]][karix + dx[k]] == 0 &&
              fie[kariy + dy[k]][karix + dx[k]] != '.') {
            fiec[kariy + dy[k]][karix + dx[k]] = 1;
            data kariba;
            kariba.y = kariy + dy[k], kariba.x = karix + dx[k],
            kariba.n = befn + 1;
            Q.push(kariba);
          }
        }
      }
    }
    while (Q.size()) {
      data kari;
      kari = Q.front(), Q.pop();
      int cou = 0;
      if (kari.y != 0 && kari.y != H - 1 && kari.x != 0 && kari.x != W - 1) {
        for (int i = 0; i < 8; i++) {
          if (fie[kari.y + dy[i]][kari.x + dx[i]] == '.')
            cou++;
        }
        if (fie[kari.y][kari.x] - '0' <= cou && fie[kari.y][kari.x] != '.') {
          lisy.push(kari.y), lisx.push(kari.x);
          // printf("[%d][%d]\\n",kari.y,kari.x);
          // for(int pp=0;pp<100000000;pp++){
          // }
        } else
          fiec[kari.y][kari.x] = 0;
      }
    }
  }
  cout << befn << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
string fie[1001];
int fiec[1001][1001];
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct data {
  int y;
  int x;
  int n;
};
queue<data> Q;
queue<int> lisy;
queue<int> lisx;
int main() {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; i++)
    cin >> fie[i];
  for (int i = 1; i < H - 1; i++) {
    for (int j = 1; j < W - 1; j++) {
      int cou = 0;
      for (int k = 0; k < 8; k++) {
        if (fie[i + dy[k]][j + dx[k]] == '.')
          cou++;
      }
      if (fie[i][j] - '0' <= cou)
        lisy.push(i), lisx.push(j);
    }
  }
  int befn = 0;
  while (1) {
    if (lisy.size() == 0)
      break;
    befn++;
    if (Q.size() == 0) {
      while (lisy.size()) {
        int kariy, karix;
        kariy = lisy.front();
        karix = lisx.front(), lisy.pop(), lisx.pop();
        fie[kariy][karix] = '.';
        for (int k = 0; k < 8; k++) {
          if (fie[kariy + dy[k]][karix + dx[k]] != '.') {
            fiec[kariy + dy[k]][karix + dx[k]] = 1;
            data kariba;
            kariba.y = kariy + dy[k], kariba.x = karix + dx[k],
            kariba.n = befn + 1;
            Q.push(kariba);
          }
        }
      }
    }
    while (Q.size()) {
      data kari;
      kari = Q.front(), Q.pop();
      int cou = 0;
      if (kari.y != 0 && kari.y != H - 1 && kari.x != 0 && kari.x != W - 1) {
        for (int i = 0; i < 8; i++) {
          if (fie[kari.y + dy[i]][kari.x + dx[i]] == '.')
            cou++;
        }
        if (fie[kari.y][kari.x] - '0' <= cou)
          lisy.push(kari.y), lisx.push(kari.x);
      }
    }
  }
  cout << befn << endl;
  return 0;
}