#P6858. 「ICPC World Finals 2021」马赛克搜索

「ICPC World Finals 2021」马赛克搜索

Description

The International Center for the Preservation of Ceramics (ICPC) is searching for motifs in some ancient mosaics. According to the ICPC's definition, a mosaic is a rectangular grid where each grid square contains a colored tile. A motif is similar to a mosaic but some of the grid squares can be empty. Figure G.1 shows an example motif and mosaic.

The rows of an rq×cqr_q \times c_q mosaic are numbered 11 to rqr_q from top to bottom, and the columns are numbered 11 to cqc_q from left to right.

A contiguous rectangular subgrid of the mosaic matches the motif if every tile of the motif matches the color of the corresponding tile of the subgrid. Formally, an rp×cpr_p \times c_p motif appears in an rq×cqr_q \times c_q mosaic at position (r,c)(r, c) if for all 1irp1 \le i \le r_p, 1jcp1 \le j \le c_p, the tile (r+i1,c+j1)(r + i - 1, c + j - 1) exists in the mosaic and either the square (i,j)(i, j) in the motif is empty or the tile at (i,j)(i, j) in the motif has the same color as the tile at (r+i1,c+j1)(r + i - 1, c + j - 1) in the mosaic.

Given the full motif and mosaic, find all occurrences of the motif in the mosaic.

G.png

Figure G.1 Motif (left) and mosaic (right) of Sample Input 1.

Input

The first line of input contains two integers rpr_p and cpc_p, where rpr_p and cpc_p (1rp,cp1 0001 \le r_p, c_p \le 1\ 000) are the number of rows and columns in the motif. Then rpr_p lines follow, each with cpc_p integers in the range [0,100][0, 100], denoting the color of the motif at that position. A value of 00 denotes an empty square.

The next line of input contains two integers rqr_q and cqc_q where rqr_q and cqc_q (1rq,cq1 0001 \le r_q, c_q \le 1\ 000) are the number of rows and columns in the mosaic. Then rqr_q lines follow, each with cqc_q integers in the range [1,100][1, 100], denoting the color of the mosaic at that position.

Output

On the first line, output kk, the total number of matches. Then output kk lines, each of the form r cr\ c where rr is the row and cc is the column of the top left tile of the match. Sort matches by increasing rr, breaking ties by increasing cc.

2 2
1 0
0 1
3 4
1 2 1 2
2 1 1 1
2 2 1 3

3
1 1
1 3
2 2