Count submatrices 3x3 with all digits 1, 2, ...,9

Given a matrix with digits 1, 2, …, 9, you must count how many submatrices of size 3×33\times 3 of the given matrix contain all different digits 1, 2, …, 9.

Input

The input has several cases. Each one starts with two positive natural numbers n,mn, m in the first line. Afterwards comes a matrix of size n×mn\times m with digits 1, 2, …, 9 (nn lines with mm digits each).

Output

For each case, the program must write in a new line the number of submatrices 3×33\times 3 inside of which all digits 1, 2, …, 9 appear.

Observation

There is no need to optimize this problem. Any reasonably efficient implementation will pass.

Evaluation over 10 points:

Problem information

Author: PRO1

Generation: 2026-01-25T21:58:13.204Z

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