Problem p03364 (C++)

Buggy submission (status: Runtime Error):
// teja349
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// setbase - cout << setbase (16); cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints
// xxx77 setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
#define f(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define ss second
#define ff first
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(a) a.size()
#define inf (1000 * 1000 * 1000 + 5)
#define all(a) a.begin(), a.end()
#define tri pair<int, pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000 * 1000 * 1000 + 7)
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>

// std::ios::sync_with_stdio(false);
string s[312], s1[312];
int n;
int check(int i) {
  int a, b, j;
  if (i < 0) {
    a = 0;
    b = i * -1;
  } else {
    a = i;
    b = 0;
  }
  rep(i, n) {
    rep(j, n) { s1[i][j] = s[(i + a) % n][(j + b) % n]; }
  }
  rep(i, n) {
    rep(j, n) {
      if (s1[i][j] != s1[j][i])
        return 0;
    }
  }
  return 1;
}
int main() {
  std::ios::sync_with_stdio(false);

  cin >> n;
  int i;
  rep(i, n) { cin >> s[i]; }
  ll ans = 0;
  f(i, 1 - n, n) {
    if (check(i))
      ans += n - abs(i);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// teja349
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// setbase - cout << setbase (16); cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints
// xxx77 setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
#define f(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define ss second
#define ff first
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(a) a.size()
#define inf (1000 * 1000 * 1000 + 5)
#define all(a) a.begin(), a.end()
#define tri pair<int, pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000 * 1000 * 1000 + 7)
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>

// std::ios::sync_with_stdio(false);
string s[312], s1[312];
int n;
int check(int i) {
  int a, b, j;
  if (i < 0) {
    a = 0;
    b = i * -1;
  } else {
    a = i;
    b = 0;
  }
  rep(i, n) {
    rep(j, n) { s1[i][j] = s[(i + a) % n][(j + b) % n]; }
  }
  rep(i, n) {
    rep(j, n) {
      if (s1[i][j] != s1[j][i])
        return 0;
    }
  }
  return 1;
}
int main() {
  std::ios::sync_with_stdio(false);

  cin >> n;
  int i;
  rep(i, n) {
    cin >> s[i];
    s1[i] = s[i];
  }
  ll ans = 0;
  f(i, 1 - n, n) {
    if (check(i))
      ans += n - abs(i);
  }
  cout << ans << endl;
  return 0;
}