Problem p01416 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define lp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) lp(i, 0, n)
#define rlp(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define rrep(i, n) rlp(i, 0, n)

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(c) begin(c), end(c)

typedef long long ll;
typedef unsigned long long ull;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

template <typename T> inline bool chmin(T &a, T b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <typename T> inline bool chmax(T &a, T b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <typename T> inline void unique(T &c) {
  sort(all(c));
  c.erase(unique(all(c)), end(c));
}

template <typename T> inline T sq(T a) { return a * a; }

template <typename T> inline size_t index_of(vector<T> &c, T x) {
  return lower_bound(all(c), x) - begin(c);
}

int m, n;
vector<tuple<char, int, int>> v;
vector<int> zx, zy;
char g[128][128];

int main() {
  cin >> m >> n;
  rep(i, m) rep(j, n) {
    char c;
    cin >> c;
    if (c != '.') {
      v.eb(c, i, j);
      lp(k, -1, 2) {
        zx.pb(i + k);
        zy.pb(j + k);
      }
    }
  }
  unique(zx);
  unique(zy);
  m = zx.size();
  n = zy.size();
  memset(g, '.', sizeof(g));
  for (auto &t : v) {
    char c;
    int x, y;
    tie(c, x, y) = t;
    g[index_of(zx, x)][index_of(zy, y)] = c;
  }
  int res = 0;
  rep(k, 26) rep(x, m) rep(y, n) {
    if (g[x][y] != '.')
      continue;
    map<char, vector<pair<int, int>>> cnt;
    rep(dir, 4) {
      int xx = x, yy = y;
      while (true) {
        xx += dx[dir], yy += dy[dir];
        if (xx < 0 || xx >= m || yy < 0 || yy >= n)
          break;
        if (g[xx][yy] != '.') {
          cnt[g[xx][yy]].eb(xx, yy);
          break;
        }
      }
    }
    for (auto &t : cnt) {
      if (t.second.size() < 2)
        continue;
      for (auto &p : t.second) {
        g[p.first][p.second] = '.';
      }
      res += 2;
    }
  }

  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define lp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) lp(i, 0, n)
#define rlp(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define rrep(i, n) rlp(i, 0, n)

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(c) begin(c), end(c)

typedef long long ll;
typedef unsigned long long ull;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

template <typename T> inline bool chmin(T &a, T b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <typename T> inline bool chmax(T &a, T b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <typename T> inline void unique(T &c) {
  sort(all(c));
  c.erase(unique(all(c)), end(c));
}

template <typename T> inline T sq(T a) { return a * a; }

template <typename T> inline size_t index_of(vector<T> &c, T x) {
  return lower_bound(all(c), x) - begin(c);
}

int m, n;
vector<tuple<char, int, int>> v;
vector<int> zx, zy;
char g[160][160];

int main() {
  cin >> m >> n;
  rep(i, m) rep(j, n) {
    char c;
    cin >> c;
    if (c != '.') {
      v.eb(c, i, j);
      lp(k, -1, 2) {
        zx.pb(i + k);
        zy.pb(j + k);
      }
    }
  }
  unique(zx);
  unique(zy);
  m = zx.size();
  n = zy.size();
  memset(g, '.', sizeof(g));
  for (auto &t : v) {
    char c;
    int x, y;
    tie(c, x, y) = t;
    g[index_of(zx, x)][index_of(zy, y)] = c;
  }
  int res = 0;
  rep(k, 26) rep(x, m) rep(y, n) {
    if (g[x][y] != '.')
      continue;
    map<char, vector<pair<int, int>>> cnt;
    rep(dir, 4) {
      int xx = x, yy = y;
      while (true) {
        xx += dx[dir], yy += dy[dir];
        if (xx < 0 || xx >= m || yy < 0 || yy >= n)
          break;
        if (g[xx][yy] != '.') {
          cnt[g[xx][yy]].eb(xx, yy);
          break;
        }
      }
    }
    for (auto &t : cnt) {
      if (t.second.size() < 2)
        continue;
      for (auto &p : t.second) {
        g[p.first][p.second] = '.';
      }
      res += 2;
    }
  }

  cout << res << endl;
}