ICPC 2021 · Problem G · Mosaic Browsing

45th ICPC · Dhaka, Bangladesh

Statement

Time limit: 6 seconds

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×cqrq\times cq 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×cprp\times cp motif appears in an rq×cqrq \times cq 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.

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

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,cp10001 \le rp, cp \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,cq10001 \le rq, cq \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.

ICPC World Finals 2021 Problem G: Mosaic Browsing

Output

On the first line, output kk, the total number of matches. Then output kk lines, each of the form rcr 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.

Sample Input 1

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

Sample Output 1

3
1 1
1 3
2 2

ICPC World Finals 2021 Problem G: Mosaic Browsing

No official solution in the source collection.