#C8455. Distinct Submatrices

    ID: 52439 Type: Default 1000ms 256MiB

Distinct Submatrices

Distinct Submatrices

Given an N×M matrix where each cell contains a single lowercase English letter, your task is to calculate the number of distinct submatrices that can be formed from this matrix.

A submatrix is defined by choosing a contiguous block of rows and contiguous block of columns. For example, if you are given the matrix:

[ \begin{bmatrix} a & b & c \ d & e & f \end{bmatrix} ]

some possible submatrices include \( [\begin{smallmatrix} a \end{smallmatrix}] \), \( [\begin{smallmatrix} b & c \end{smallmatrix}] \), and \( [\begin{smallmatrix} a & b \\ d & e \end{smallmatrix}] \). Note that two submatrices are considered distinct if their content differs.

Your solution should use a brute-force approach to generate every possible submatrix and count the number of distinct ones. The answer is guaranteed to fit in a 32-bit signed integer. The problem assumes the constraints are small enough for a brute-force solution to pass.

Input/Output format: Your program must read input from standard input and write output to standard output.

inputFormat

The first line of input contains two space‐separated integers \(N\) and \(M\) representing the number of rows and columns respectively.

The following \(N\) lines each contain a string of \(M\) lowercase English letters. These \(N\) strings represent the rows of the matrix.

It is guaranteed that \(1 \leq N, M \leq 50\) (or a similar small bound) so that a brute-force approach is feasible.

outputFormat

Output a single integer representing the number of distinct submatrices of the given matrix.

## sample
2 3
abc
def
18