Cipher

Time limit1sMemory limit128 MB

Summary
Find the a x b subarray that occurs exactly k times (k >= 3) in an n x m character grid and list all its top-left positions in row-major order.
Level

Hard8 of 10

Topics
Hash map, String, Implementation, Sorting
Solved
No attempts yet

Problem

While exploring the Universe, a space agency found traces of extraterrestrial (ET) intelligence: a collection of rectangular metal plates carrying messages written in an alien language.

Each plate holds a 2D array with nn rows and mm columns. Every cell is a printable ASCII character whose numeric code lies in the range 32…12732 \dots 127. Each plate also carries two integers, aa and bb.

The researchers concluded that a message can be decrypted with a cipher: a key, hidden inside the plate's array, that reveals how to read the message. The cipher is the a×ba \times b rectangular subarray of the message that occurs exactly kk times, with k≥3k \ge 3. Occurrences of the cipher may overlap. It is guaranteed that no other a×ba \times b subarray occurs more than k−2k - 2 times, so the cipher is unique.

For example, suppose the array is 8×108 \times 10 (n=8n = 8, m=10m = 10), the cipher size is 3×33 \times 3 (a=3a = 3, b=3b = 3), and the cipher occurs 55 times (k=5k = 5). Then no other 3×33 \times 3 subarray of this array occurs more than 33 times.

Given the array that represents the message and the integers aa and bb written on the plate, find the cipher and all positions on the plate where it occurs.

Input

The first line contains two integers nn and mm, separated by a space. Each of the next nn lines contains a string of exactly mm characters; the ii-th of these lines is row ii of the array. The last line contains the two integers aa and bb, separated by a space.

Output

On the first line print the integers aa and bb, separated by a space (exactly the values from the input). Then print aa lines, each a string of bb characters, giving the cipher. On the next line print the integer kk, the number of times the cipher occurs in the array. Then print kk lines, each containing two integers: the row and the column (1-indexed) of the upper-left corner of one occurrence of the cipher. Print these kk pairs sorted in increasing order of row, breaking ties by increasing column.

Constraints

  • 5≤n,m≤10005 \le n, m \le 1000
  • 2≤a≤n2 \le a \le n
  • 2≤b≤m2 \le b \le m
  • 3≤k≤10003 \le k \le 1000
  • Rows and columns are numbered from 11, starting at the upper-left corner.

Hint

An example array, with the four occurrences of its cipher highlighted, is shown below:

Examples3

  1. Example 1

    Input
    8 10
    qw.aba..f.
    wq.bab.ff.
    zx.cdc.K.R
    c.ababa.es
    x.babab.Ed
    j.cdcdcaba
    yo.k.k.bab
    opu..l.cdc
    3 3
    
    Expected output
    3 3
    aba
    bab
    cdc
    4
    1 4
    4 3
    4 5
    6 8
    
  2. Example 2

    Input
    5 5
    XYkXY
    ZWBZW
    72#V+
    8XYed
    UZW06
    2 2
    
    Expected output
    2 2
    XY
    ZW
    3
    1 1
    1 4
    4 2
    
  3. Example 3

    Input
    5 6
    #####8
    #####&
    Dy8%&8
    YtDt!X
    biu<fM
    2 2
    
    Expected output
    2 2
    ##
    ##
    4
    1 1
    1 2
    1 3
    1 4