This page is still under construction.

Parts of this page are still being built. What you see may change.

Finding a Grayscale Image

Time limit10sMemory limit512 MB

Summary
Count the R by C windows of A that equal B after a linear brightness change of the form p times A plus q.
Level

Hard8 of 10

Topics
String matching, Matrix, Math
Solved
No attempts yet

Problem

A grayscale image has no color. Each pixel of a grayscale image therefore records one number, the brightness of that pixel. In this problem the brightness of a pixel is an integer between 00 and 6553565535.

A grayscale image II of size H×WH \times W is H×WH \times W pixels arranged in HH rows and WW columns. Write I[i,j]I[i, j] for the brightness of the pixel in row ii and column jj. (1≤i≤H1 \le i \le H, 1≤j≤W1 \le j \le W)

Gyeonggeun has a grayscale image AA of size N×MN \times M and a grayscale image BB of size R×CR \times C. Here N≥RN \ge R and M≥CM \ge C, so AA is no shorter and no narrower than BB. Gyeonggeun believes that AA plagiarized BB, and wants to count how many parts of AA resemble BB.

The counting works like this. First choose one rectangle of R×CR \times C pixels inside the grayscale image AA. The rectangle is determined by the position (x,y)(x, y) of the pixel at its top left corner. (1≤x≤N−R+11 \le x \le N - R + 1, 1≤y≤M−C+11 \le y \le M - C + 1)

The size is fixed, so moving the top left corner moves the whole rectangle with it. For example, take the grayscale image above as AA and choose a 4×64 \times 6 rectangle whose top left corner is (4,4)(4, 4). The bottom right corner of that rectangle is (4+4−1,4+6−1)=(7,9)(4 + 4 - 1, 4 + 6 - 1) = (7, 9).

Whether the chosen rectangle resembles the image BB is decided by the following rule.

If there are real numbers pp and qq such that p×A[x+i−1,y+j−1]+q=B[i,j]p \times A[x + i - 1, y + j - 1] + q = B[i, j] holds for every ii and jj with 1≤i≤R1 \le i \le R and 1≤j≤C1 \le j \le C, then the rectangle chosen in AA and the image BB are similar.

Apply this rule to every rectangle of size R×CR \times C in AA and count the rectangles that are similar to BB. In other words, apply the rule to every (x,y)(x, y) with 1≤x≤N−R+11 \le x \le N - R + 1 and 1≤y≤M−C+11 \le y \le M - C + 1, then count the pairs (x,y)(x, y) judged similar. Two rectangles of the same size are different when the coordinates of their top left corners differ, no matter what the brightness inside them is.

Help Gyeonggeun and write a program that counts the parts of AA that are similar to BB.

Input

The first line contains the number of rows NN and the number of columns MM of the grayscale image AA, separated by a space. (1≤N,M≤10001 \le N, M \le 1000)

The next NN lines describe the pixels of AA. The ii-th of those lines contains MM integers A[i,1],A[i,2],…,A[i,M]A[i, 1], A[i, 2], \dots, A[i, M] separated by spaces. The jj-th integer on the ii-th line is the brightness of the pixel in row ii and column jj of AA. (1≤i≤N1 \le i \le N)

The next line contains the number of rows RR and the number of columns CC of the grayscale image BB, separated by a space. (1≤R≤N1 \le R \le N, 1≤C≤M1 \le C \le M)

The next RR lines describe the pixels of BB. The ii-th of those lines contains CC integers B[i,1],B[i,2],…,B[i,C]B[i, 1], B[i, 2], \dots, B[i, C] separated by spaces. (1≤i≤R1 \le i \le R)

Every given pixel brightness is an integer between 00 and 6553565535.

Output

Print the number of parts of AA that are similar to BB.

Hint

When every pixel of BB has the same brightness, you can take p=0p = 0 and let qq be that brightness, so every rectangle of AA is judged similar.

Examples8

  1. Example 1

    Input
    4 4
    1 2 3 4
    2 1 4 3
    3 4 1 2
    4 3 2 1
    2 2
    5 6
    6 5
    
    Expected output
    5
    
  2. Example 2

    Input
    5 5
    9 2 5 4 3
    6 8 2 0 2
    4 3 6 4 3
    7 2 3 4 8
    8 2 4 6 7
    3 3
    77 77 77
    77 77 77
    77 77 77
    
    Expected output
    9
    
  3. Example 3

    Input
    1 1
    42
    1 1
    7
    
    Expected output
    1
    
  4. Example 4

    Input
    3 3
    0 1 2
    3 4 5
    6 7 8
    1 1
    65535
    
    Expected output
    9
    
  5. Example 5

    Input
    3 4
    100 100 100 100
    100 100 100 100
    100 100 100 100
    2 2
    1 2
    3 4
    
    Expected output
    0
    
  6. Example 6

    Input
    3 4
    2 3 4 5
    5 4 3 2
    1 3 5 7
    2 2
    5 4
    2 3
    
    Expected output
    1
    
  7. Example 7

    Input
    1 7
    0 3 6 9 12 15 18
    1 2
    0 1
    
    Expected output
    6
    
  8. Example 8

    Input
    2 3
    0 65535 0
    65535 0 65535
    2 2
    65535 0
    0 65535
    
    Expected output
    2