#P2427. Wave

Wave

Description

The propagation speed of waves differs across media. In vacuum, all wave speeds are 3×1083\times {10}^8 m/s, while in liquid media the wave speed is lower than in vacuum and also varies between different liquid media. We divide a liquid surface into an N×MN \times M grid of equal-sized square cells, each cell containing exactly one liquid medium. We want to know, for a wave emitted from some source, how large an axis-aligned square centered at the source the wave can extend to while maintaining an unchanged wave speed. Assume all such squares are axis-aligned with the coordinate axes.

Input Format

The first line contains three integers N,M,QN, M, Q, denoting the number of rows and columns of the grid and the number of queries, respectively. Then follows an N×MN \times M matrix in which each element is a lowercase letter representing the medium in the corresponding cell; different letters denote different media. Then follow QQ lines, each with two integers XX and YY describing a query: the side length of the largest axis-aligned square centered at row XX, column YY within which the wave can extend while maintaining an unchanged wave speed is requested. Rows are numbered from 00 to N1N-1, and columns from 00 to M1M-1.

Output Format

Output QQ lines, each containing one integer, the answer to the corresponding query.

5 5 3
abbaa
abbaa
aaaaa
aaaaa
aaaaa
1 2
1 4
3 2

1
1
3

Hint

For 30%30\% of the testdata, 1N,M501 \le N, M \le 50, 1Q5001 \le Q \le 500.

For 100%100\% of the testdata, 1N,M10001 \le N, M \le 1000, 1Q100001 \le Q \le 10000.

Translated by ChatGPT 5