#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <queue>
#include <cstdint>
#include <algorithm>
#include <set>
#include <unordered_set>
const int32_t alph_size = 26;
struct Node {
int32_t link=-1;
int32_t deep=0;
int32_t number=0;
std::vector<int32_t> jump;
};
struct AhoKorasik {
std::vector<Node> nodes;
int32_t cur_words = 0;
AhoKorasik() {
nodes.push_back(Node());
nodes[0].link = 0;
nodes[0].jump.resize(alph_size, -1);
nodes[0].deep = 0;
}
void AddString(const std::string& str) {
int32_t cur_vertex = 0;
bool flag = false;
for (auto symbol : str) {
int32_t symbol_index = symbol - 'a';
if (nodes[cur_vertex].jump[symbol_index] == -1) {
nodes[cur_vertex].jump[symbol_index] = nodes.size();
nodes.push_back(Node());
nodes.back().link = -1;
nodes.back().jump.resize(alph_size, -1);
nodes.back().deep = nodes[cur_vertex].deep + 1;
flag = true;
}
cur_vertex = nodes[cur_vertex].jump[symbol_index];
}
if (flag) {
nodes[cur_vertex].number = ++cur_words;
}
}
void Build() {
std::queue<int32_t> que;
for (char c = 'a'; c <= 'z'; ++c) {
int32_t symbol = c - 'a';
int32_t& child = nodes[0].jump[symbol];
if (child != -1) {
nodes[child].link = 0;
que.push(child);
}
else {
child = 0;
}
}
while (!que.empty()) {
int32_t vertex = que.front();
que.pop();
for (char c = 'a'; c <= 'z'; ++c) {
int32_t symbol = c - 'a';
int32_t& child = nodes[vertex].jump[symbol];
if (child != -1) {
que.push(child);
}
else {
child = nodes[nodes[vertex].link].jump[symbol];
}
}
for (char c = 'a'; c <= 'z'; ++c) {
int32_t symbol = c - 'a';
int32_t& child = nodes[vertex].jump[symbol];
if (nodes[child].link == -1) {
nodes[child].link = nodes[nodes[vertex].link].jump[symbol];
}
}
}
}
int32_t Go(int32_t vertex, int32_t symbol) {
return nodes[vertex].jump[symbol];
}
int32_t GetLink(int32_t vertex) {
return nodes[vertex].link;
}
};
std::pair<std::string, std::string> GetTextAndPattern(const std::vector<std::string>& text, const std::vector<std::string>& pattern) {
int32_t text_height = text.size();
int32_t text_width = text[0].size();
int32_t pattern_height = pattern.size();
int32_t pattern_width = pattern[0].size();
std::vector<std::vector<int16_t>> transformed_text(text_width, std::vector<int16_t>(text_height));
std::string text_gorizontal;
std::string pattern_gorizontal;
AhoKorasik aho_gorizontal;
for (auto& str : pattern) {
aho_gorizontal.AddString(str);
}
aho_gorizontal.Build();
int32_t cur_vertex = 0;
for (int32_t i = 0; i < text_height; ++i) {
for (int32_t j = 0; j < text_width; ++j) {
cur_vertex = aho_gorizontal.Go(cur_vertex, text[i][j] - 'a');
transformed_text[j][i] = aho_gorizontal.nodes[cur_vertex].number;
}
cur_vertex = 0;
}
for (int32_t i = pattern_width - 1; i < text_width; ++i) {
for (auto c : transformed_text[i]) {
text_gorizontal += '0' + c;
}
text_gorizontal += '#';
}
for (int32_t i = 0; i < pattern_height; ++i) {
for (int32_t j = 0; j < pattern_width; ++j) {
cur_vertex = aho_gorizontal.Go(cur_vertex, pattern[i][j] - 'a');
}
pattern_gorizontal += '0' + aho_gorizontal.nodes[cur_vertex].number;
cur_vertex = 0;
}
return { text_gorizontal, pattern_gorizontal };
}
void Transpose(std::vector<std::string>& text) {
std::vector<std::string> transposed(text[0].size(), std::string(text.size(), 0));
for (int32_t i = 0; i < text.size(); ++i) {
for (int32_t j = 0; j < text[0].size(); ++j) {
transposed[j][i] = text[i][j];
}
}
text = std::move(transposed);
}
std::vector<int32_t> BuildZ(std::string text) {
std::vector<int32_t> z(text.size());
int32_t n = text.size();
for (int32_t i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r)
z[i] = std::min(r - i + 1, z[i - l]);
while (i + z[i] < n && text[z[i]] == text[i + z[i]])
++z[i];
if (i + z[i] - 1 > r)
l = i, r = i + z[i] - 1;
}
return z;
}
int32_t ans = 0;
void GetAnswer(std::string& text, std::string& pattern, int32_t text_height, int32_t pattern_width, std::vector<std::vector<bool>>& lu_corner, bool rev = false) {
auto default_pref = BuildZ(pattern + "*" + text);
std::reverse(text.begin(), text.end());
std::reverse(pattern.begin(), pattern.end());
auto reversed_pref = BuildZ(pattern + "*" + text);
std::reverse(text.begin(), text.end());
std::reverse(pattern.begin(), pattern.end());
int32_t cur_word = 0;
int32_t text_pos;
for (int32_t i = pattern.size() + 1; i < default_pref.size() - pattern.size(); ++i) {
text_pos = i - pattern.size() - 1;
if (text[text_pos] == '#') {
++cur_word;
continue;
}
if (default_pref[i] == pattern.size()) {
if (!rev) {
++ans;
}
continue;
}
if (text[text_pos + default_pref[i]] == '#') {
continue;
}
int32_t mismatch_symbol = text_pos + default_pref[i];
int32_t mismatch_symbol_rev = pattern.size() + 1 + text.size() - mismatch_symbol;
int32_t remaining_pattern_size = pattern.size() - default_pref[i] - 1;
if (default_pref[i] + reversed_pref[mismatch_symbol_rev - 1 - remaining_pattern_size] + 1 == pattern.size()) {
int32_t y = cur_word;
int32_t x = (text_pos - cur_word) % text_height;
if (!rev) {
lu_corner[x][y] = true;
}
else if (lu_corner[y][x]) {
++ans;
}
}
}
}
void Print(std::vector<std::string>& vec) {
for (auto v : vec) {
std::cout << v << '\n';
}
}
int32_t main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int32_t text_height;
int32_t text_width;
std::cin >> text_height >> text_width;
std::vector<std::string> text(text_height);
for (auto& str : text) {
std::cin >> str;
}
int32_t pattern_height;
int32_t pattern_width;
std::cin >> pattern_height >> pattern_width;
std::vector<std::string> pattern(pattern_height);
for (auto& str : pattern) {
std::cin >> str;
}
std::vector<std::vector<bool>> lu_corner(text_height, std::vector<bool>(text_width, false));
auto pair = GetTextAndPattern(text, pattern);
GetAnswer(pair.first, pair.second, text_height, pattern_width, lu_corner);
std::cout << pair.first << '\n' << pair.second << '\n';
Transpose(text);
Transpose(pattern);
pair = GetTextAndPattern(text, pattern);
GetAnswer(pair.first, pair.second, text_width, pattern_height, lu_corner, true);
std::cout << ans << '\n';
}