Count submatrices of sizes 1x9 and 9x1 which contain all digits 1, 2, ..., 9.

Given a matrix made of digits 1, 2, …, 9, we must count how many
submatrices 1 × 9 (consecutive subrow of size 9), and how many
submatrices 9 × 1 (consecutive subcolumn of size 9) contain all digits
1, 2, …, 9.

For instance, pay attention to this matrix:

    1234567891
    2345678912
    3456789123
    4567891234
    5678912345
    6789123456
    7891234567
    8912345678
    9123456789

At each row there are two subrows of size 9 with all digits, and at
every column there is one subcolumn of size 9 with all digits (the
column itself is the subcolumn). Therefore, in this case the answer
would be 18 10.

Input

The input has several cases. Each case starts with two numbers n, m in
the first line, followed by a matrix of size n × m made of digits 1, 2,
…, 9 (i.e. n lines with m digits each).

Output

For each case, the program must write in a new line the number of
subrows and number of subcolumns of size 9 where all digits 1, 2, …, 9
appear.

Observation

There is no need to optimize, since any reasonable implementation will
pass the private tests.

Evaluation over 10 points:

- Slow solution: 5 points.

- Fast solution: 10 points.

We define a fast solution as one which is correct, has linear cost and
passes both public and private test cases. We define as a slow solution
one which is not fast, but it is correct and passes the public test
cases.

Problem information

Author: PRO1

Generation: 2026-01-25T23:05:51.554Z

© Jutge.org, 2006–2026.
https://jutge.org
