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 JavaScript (judge verdict: Time Limit Exceeded)

//config = { input: 'tmp', newline: '\r\n' }; // win
config = { input: '/dev/stdin', newline: '\n' }; // linux

line = require('fs').readFileSync(config.input, 'ascii')
  .split(config.newline);
line[0] = line[0].split(' ').map(Number);
H = line[0][0];
W = line[0][1];
field = line.slice(1, 1+H);
line[1+H] = line[1+H].split(' ').map(Number);
R = line[1+H][0];
C = line[1+H][1];
pat = line.slice(1+H+1, 1+H+1+R);

if (H < R || W < C) process.exit();

mem = new Array(H-R+1);
for (i = 0; i <= H-R; i++) {
  mem[i] = new Array(W-C+1);
  for (j = 0; j <= W-C; j++) mem[i][j] = 0;
}

match_count = new Array(W-C+1);
match = new Array(W-C+1);
for (i = 0; i <= W-C; i++) {
  match_count[i] = 0;
  match[i] = false;
}

function string_match(str1, str2, W, C) {
  var i, j, k;
  for (i in match_count) match_count[i] = 0;
  for (i = 0; i <= W-C; i++) {
    for (j = 0; j < C; j++) {
      if (str1[i+j] === str2[j]) match_count[i]++;
    }
  }
  for (i in match) match[i] = (match_count[i] === C);
}

for (i = 0; i < H; i++) {
  for (j = Math.max(0, i-H+R); j < R && j <= i; j++) {
    string_match(field[i], pat[j], W, C);
    for (k in match) {
      if (!match[k]) continue;
      mem[i-j][k]++;
    }
  }
}

for (i in mem)
  for (j in mem[i])
    if (mem[i][j] === R) console.log('%s %s', i, j);