Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int v[4][2] = {
    {0, 1},
    {1, 0},
    {-1, 0},
    {0, -1},
};

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

  int count = h * w;
  vector<pair<int, int>> s1;
  s1.reserve(h * w);
  vector<pair<int, int>> s2;
  s2.reserve(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == '#') {
        s1.push_back(make_pair(i, j));
        count--;
      }
    }
  }

  int c = 0;
  while (count > 0) {
    while (!s1.empty()) {
      auto p = s1.back();
      //        for(int i = 0; i < s1.size(); i++){
      //            cerr << s1[i].first << " " << s1[i].second << endl;
      //        }
      //        cerr << endl;
      s1.pop_back();
      for (auto &i : v) {
        if (p.first + i[0] < 0 || h <= p.first + i[0] || p.second + i[1] < 0 ||
            w <= p.second + i[1]) {
          continue;
        }
        //                cerr << p.first + first+ i[0] << " " << p.second +
        //                i[1] << endl;
        if (a[p.first + i[0]][p.second + i[1]] == '.') {
          a[p.first + i[0]][p.second + i[1]] = '#';
          count--;
          s2.push_back(make_pair(p.first + i[0], p.second + i[1]));
        }
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cerr << a[i][j] << " ";
      }
      cerr << endl;
    }
    s1.clear();
    copy(s2.begin(), s2.end(), back_inserter(s1));
    s2.clear();
    c++;
  }
  cout << c << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int v[4][2] = {
    {0, 1},
    {1, 0},
    {-1, 0},
    {0, -1},
};

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

  int count = h * w;
  vector<pair<int, int>> s1;
  s1.reserve(h * w);
  vector<pair<int, int>> s2;
  s2.reserve(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == '#') {
        s1.push_back(make_pair(i, j));
        count--;
      }
    }
  }

  int c = 0;
  while (count > 0) {
    while (!s1.empty()) {
      auto p = s1.back();
      //        for(int i = 0; i < s1.size(); i++){
      //            cerr << s1[i].first << " " << s1[i].second << endl;
      //        }
      //        cerr << endl;
      s1.pop_back();
      for (auto &i : v) {
        if (p.first + i[0] < 0 || h <= p.first + i[0] || p.second + i[1] < 0 ||
            w <= p.second + i[1]) {
          continue;
        }
        //                cerr << p.first + first+ i[0] << " " << p.second +
        //                i[1] << endl;
        if (a[p.first + i[0]][p.second + i[1]] == '.') {
          a[p.first + i[0]][p.second + i[1]] = '#';
          count--;
          s2.push_back(make_pair(p.first + i[0], p.second + i[1]));
        }
      }
    }
    // for(int i = 0; i < h; i++) {
    //     for(int j = 0; j < w; j++) {
    //         cerr << a[i][j] << " ";
    //     }
    //     cerr << endl;
    // }
    s1.clear();
    copy(s2.begin(), s2.end(), back_inserter(s1));
    s2.clear();
    c++;
  }
  cout << c << endl;

  return 0;
}