This page is still under construction.

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

Best Position

Time limit10sMemory limit256 MB

Summary
For each binary blueprint, find the placement with the most matching cells, tie-broken by row then column, and report the grain and livestock counts.
Level

Medium7 of 10

Topics
String matching, Matrix, Brute force
Solved
No attempts yet

Problem

A farmer plans a new farm on a large field. The field is a grid with RR rows and CC columns, and each cell produces one kind of food: grain (G) or livestock (L). Here is a field with R=5R = 5 and C=8C = 8.

  12345678
1 GLGGLGLG
2 GGLGGLGL
3 GGLLLGGG
4 LLGLLGLG
5 LGGGLGLL

The farmer also has blueprints. A blueprint is a grid with HH rows and WW columns, where H≤RH \le R and W≤CW \le C. Each blueprint cell names the food the farmer wants in that spot, grain (G) or livestock (L). Here is a blueprint with H=2H = 2 and W=3W = 3.

  123
1 GLL
2 LGG

The farmer builds the farm by laying a blueprint on the field. The placement is named by the position (r,c)(r, c) of its top-left corner, so the blueprint covers field rows rr through r+H−1r + H - 1 and columns cc through c+W−1c + W - 1. The whole blueprint has to fit inside the field, so r+H−1≤Rr + H - 1 \le R and c+W−1≤Cc + W - 1 \le C. For 0≤i<H0 \le i < H and 0≤j<W0 \le j < W, the field cell at (r+i,c+j)(r + i, c + j) produces food when its kind of food is the same as the kind in the blueprint cell at (i+1,j+1)(i + 1, j + 1).

The farmer wants the placement that produces the most food, counting grain and livestock together. When several placements produce the same amount, he takes the one with the smallest rr, and among those the one with the smallest cc.

For the field and the blueprint above the best placement is (1,3)(1, 3). The blueprint then covers rows 1 to 2 and columns 3 to 5, where the field reads GGL on top and LGG below. Blueprint row GLL agrees with the field in the first and the third cell, producing 1 grain and 1 livestock. Blueprint row LGG agrees in all three cells, producing 2 grains and 1 livestock. The placement produces 5 foods, 3 grains and 2 livestock. Placements (2,5)(2, 5) and (3,2)(3, 2) also produce 5 foods, but (1,3)(1, 3) has the smaller row number, and every other placement produces fewer than 5 foods.

Input

The input describes one field. The first line has two integers RR and CC (1≤R,C≤5001 \le R, C \le 500). Each of the next RR lines has CC characters and describes one row of the field.

The next line has one integer BB (1≤B≤51 \le B \le 5), the number of blueprints the farmer has. Then come BB blueprints. Each blueprint starts with a line holding two integers HH and WW (1≤H≤R1 \le H \le R, 1≤W≤C1 \le W \le C), followed by HH lines of WW characters each.

Every character of the field and of the blueprints is either G or L.

Output

Print one line for each blueprint, in input order. For the blueprint numbered XX, counting from 1, print Case #X: followed by four integers separated by single spaces. The first two are the row and the column of the best position for the farm. The last two are the number of grains and the number of livestock produced at that position.

Examples1

  1. Example 1

    Input
    5 8
    GLGGLGLG
    GGLGGLGL
    GGLLLGGG
    LLGLLGLG
    LGGGLGLL
    3
    2 3
    GLL
    LGG
    3 1
    L
    G
    G
    1 4
    GGLL
    
    Expected output
    Case #1: 1 3 3 2
    Case #2: 1 2 2 1
    Case #3: 3 1 2 2