#include #include #include using namespace std; const int maxn = 510; const int mod = 5201314; int dp[2][maxn][maxn], n ,m; char maps[maxn][maxn]; int main () { //freopen("in.txt","r",stdin); int Case; scanf("%d",&Case); while (Case --) { scanf("%d",&n); m = n; for (int i=1; i<=n; i++) scanf ("%s", maps[i]+1); if (maps[1][1] != maps[n][m]) { printf ("0\n"); continue; } memset (dp, 0, sizeof(dp)); dp[0][1][n] = 1; int res = (n + m - 2) / 2; for (int i=1; i<=res; i++) { for (int j=1; j<=i+1; j++) for (int k=n; k>=n-i; k--) { int x1, x2, y1, y2; x1 = j, y1 = i - j + 2; x2 = k, y2 = n + m - k - i; if (maps[x1][y1] == maps[x2][y2]) { if (x1>x2 || y1>y2) continue; dp[i%2][j][k] = (dp[i%2][j][k] + dp[(i%2)^1][j-1][k])%mod; dp[i%2][j][k] = (dp[i%2][j][k] + dp[(i%2)^1][j-1][k+1])%mod; dp[i%2][j][k] = (dp[i%2][j][k] + dp[(i%2)^1][j][k])%mod; dp[i%2][j][k] = (dp[i%2][j][k] + dp[(i%2)^1][j][k+1])%mod; } } memset(dp[(i%2)^1], 0, sizeof(dp[(i%2)^1])); } int ans = 0; if ((n+m)%2 == 0) { for (int i=1; i<=n; i++) ans = (ans + dp[res%2][i][i]) % mod; } else for (int i=1; i<=n; i++) { int x = res - i + 2; if (x + 1 <= m) ans = (ans + dp[res%2][i][i]) % mod; if (i + 1 <= n) ans = (ans + dp[res%2][i][i+1]) % mod; } printf ("%d\n", ans); } return 0; }