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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1}, dy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
void renew(string &x, string y) {
  if (x.size() > y.size())
    return;
  if (x.size() < y.size()) {
    x = y;
    return;
  }
  if (x > y)
    x = y;
}
int main() {
  while (true) {
    int H, W;
    cin >> H >> W;
    if (H == 0)
      break;
    string s[10];
    rep(i, H) cin >> s[i];
    string ans;
    rep(ai, H) rep(aj, W) rep(ad, 8) {
      // show(ai);
      // show(aj);
      // show(ad);
      string sa;
      int ax = ai, ay = aj;
      do {
        sa += s[ax][ay];
        ax += dx[ad], ay += dy[ad];
        if (ax == H)
          ax = 0;
        if (ay == W)
          ay = 0;
        if (ax == -1)
          ax = H - 1;
        if (ay == -1)
          ay = W - 1;
        // show(ax);
        // show(ay);
      } while (!(ax == ai && ay == aj));
      // show(sa);
      rep(bi, H) rep(bj, W) rep(bd, 8) {
        if (s[ai][aj] != s[bi][bj])
          break;
        if (ai == bi && aj == bj && ad == bd)
          continue;
        // show(bi);
        // show(bj);
        // show(bd);
        int bx = bi, by = bj, cnt = 0;
        do {
          if (cnt >= sa.size() || sa[cnt] != s[bx][by])
            break;
          cnt++;
          bx += dx[bd], by += dy[bd];
          if (bx == H)
            bx = 0;
          if (by == W)
            by = 0;
          if (bx == -1)
            bx = H - 1;
          if (by == -1)
            by = W - 1;
        } while (!(bx == bi && by == bj));
        renew(ans, sa.substr(0, cnt));
      }
    }
    if (ans.size() <= 1)
      puts("0");
    else
      cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1}, dy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
void renew(string &x, string y) {
  if (x.size() > y.size())
    return;
  if (x.size() < y.size()) {
    x = y;
    return;
  }
  if (x > y)
    x = y;
}
int main() {
  while (true) {
    int H, W;
    cin >> H >> W;
    if (H == 0)
      break;
    string s[10];
    rep(i, H) cin >> s[i];
    string ans;
    rep(ai, H) rep(aj, W) rep(ad, 8) {
      // show(ai);
      // show(aj);
      // show(ad);
      string sa;
      int ax = ai, ay = aj;
      do {
        sa += s[ax][ay];
        ax += dx[ad], ay += dy[ad];
        if (ax == H)
          ax = 0;
        if (ay == W)
          ay = 0;
        if (ax == -1)
          ax = H - 1;
        if (ay == -1)
          ay = W - 1;
        // show(ax);
        // show(ay);
      } while (!(ax == ai && ay == aj));
      // show(sa);
      rep(bi, H) rep(bj, W) rep(bd, 8) {
        if (ai == bi && aj == bj && ad == bd)
          continue;
        // show(bi);
        // show(bj);
        // show(bd);
        string sb;
        int bx = bi, by = bj;
        do {
          // show(bx);
          // show(by);
          sb += s[bx][by];
          bx += dx[bd], by += dy[bd];
          if (bx == H)
            bx = 0;
          if (by == W)
            by = 0;
          if (bx == -1)
            bx = H - 1;
          if (by == -1)
            by = W - 1;
        } while (!(bx == bi && by == bj));
        // show(sb);
        int u = min(sa.size(), sb.size()) + 1, l = 0;
        while (u - l > 1) {
          int m = (u + l) / 2;
          if (sa.substr(0, m) == sb.substr(0, m))
            l = m;
          else
            u = m;
        }
        renew(ans, sb.substr(0, l));
      }
    }
    if (ans.size() <= 1)
      puts("0");
    else
      cout << ans << endl;
  }
}