Problem:
Pattern Search

Find places where a R × C pattern is found within a H × W region. Print top-left coordinates (i, j) of sub-regions where the pattern found. The top-left and bottom-right coordinates of the region is (0, 0) and (H-1, W-1) respectively.

Input

In the first line, two integers H and W are given. In the following H lines, i-th lines of the region are given.

In the next line, two integers R and C are given. In the following R lines, i-th lines of the pattern are given.

output

For each sub-region found, print a coordinate i and j separated by a space character in a line. Print the coordinates in ascending order of the row numbers (i), or the column numbers (j) in case of a tie.

Constraints

1 ≤ H, W ≤ 1000

1 ≤ R, C ≤ 1000

The input consists of alphabetical characters and digits

Sample Input 1

4 5
00010
00101
00010
00100
3 2
10
01
10

Sample Output 1

0 3
1 2

Solution in C++ (judge verdict: Accepted)

/*
 * c.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int MAX_N = 1000;

typedef long long ll;

const ll BS0 = 2549;
const ll BS1 = 4283;
const ll MOD = 1000000007;

/* typedef */

/* global variables */

string s[MAX_N], p[MAX_N];
ll es0[MAX_N + 1], es1[MAX_N + 1];
ll shs[MAX_N + 1][MAX_N + 1], phs[MAX_N + 1][MAX_N + 1];

/* subroutines */

void makehash(int h, int w, ll hs[MAX_N + 1][MAX_N + 1], string s[]) {
  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      hs[y + 1][x + 1] =
	(es0[y] * es1[x] % MOD * s[y][x] % MOD +
	 hs[y][x + 1] + hs[y + 1][x] - hs[y][x] + MOD) % MOD;
}

/* main */

int main() {
  es0[0] = es1[0] = 1;
  for (int i = 1; i <= MAX_N; i++) {
    es0[i] = (es0[i - 1] * BS0) % MOD;
    es1[i] = (es1[i - 1] * BS1) % MOD;
  }

  int h, w;
  cin >> h >> w;
  for (int y = 0; y < h; y++) cin >> s[y];

  int r, c;
  cin >> r >> c;
  for (int y = 0; y < r; y++) cin >> p[y];

  makehash(h, w, shs, s);
  makehash(r, c, phs, p);

  for (int y = 0; y <= h - r; y++)
    for (int x = 0; x <= w - c; x++) {
      ll sh =
	(shs[y + r][x + c] + shs[y][x]
	 - shs[y][x + c] + MOD - shs[y + r][x] + MOD) % MOD;
      ll ph = phs[r][c] * es0[y] % MOD * es1[x] % MOD;
      if (sh == ph) printf("%d %d\n", y, x);
    }
  return 0;
}