#include <cstdio> #include <cstdlib> #include <string> #include <iostream> #include <cstring> using namespace std; const int maxn = 100; enum DIR { RIGHT, LOW, RLOW }; int dfs(char maze[maxn][maxn], int m, int n, string s, string find, int idx, int x, int y, DIR d) { if (s == find) return 1; if (idx >= find.size()) return 0; if (s[idx] != find[idx]) return 0; int right = 0, low = 0, rlow = 0; if (d == RIGHT && y + 1 < n) { right = dfs(maze, m, n, s + maze[x][y + 1], find, idx + 1, x, y + 1, RIGHT); } if (d == LOW && x + 1 < m) { low = dfs(maze, m, n, s + maze[x + 1][y], find, idx + 1, x + 1, y, LOW); } if (d == RLOW && x + 1 < m && y + 1 < n) { rlow = dfs(maze, m, n, s + maze[x + 1][y + 1], find, idx + 1, x + 1, y + 1, RLOW); } return right + low + rlow; } int main() { int t; char maze[maxn][maxn]; string find; cin >> t; while (t--) { memset(maze, 0, sizeof(maze)); int m, n; cin >> m >> n; for (int i = 0; i < m; ++i) { cin >> maze[i]; } cin >> find; int ans = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { string s(""); int r = dfs(maze, m, n, s + maze[i][j], find, 0, i, j, RIGHT); int l = dfs(maze, m, n, s + maze[i][j], find, 0, i, j, LOW); int rl = dfs(maze, m, n, s + maze[i][j], find, 0, i, j, RLOW); ans = ans + r + l + rl; } } cout << ans << endl; } return 0; }