ssdj

Time limit: 0.45s Memory limit: 128MB Input: ssdj.in Output: ssdj.out

Because not everyone scored a 10 on the mock exam, the school administration decided to punish the students in an inhumane way: they were no longer allowed to go to the theater or read works by the great literary classics. Their only solace was a matrix with NN rows and NN columns containing only lowercase letters of the English alphabet, for which they had to identify the valid submatrices. A submatrix is considered valid if it simultaneously meets the following conditions:

  • It has at least two rows and at least two columns.
  • The letters in the top-left and bottom-right corners of the submatrix are lexicographically larger (strictly) than all other letters in the submatrix.

Task

Help the high school students find the number of valid submatrices in the matrix and thus escape the horrible punishment.

Input data

The file ssdj.in contains on the first line the natural number NN, and on the next NN lines, there are NN lowercase letters each, not separated by spaces.

Output data

The file ssdj.out contains a single natural number representing the number of valid submatrices.

Constraints and Notes

  • 1N1 0001 \leq N \leq 1\ 000
  • For tests worth 1010 points, N50N \leq 50
  • For tests worth 2020 points, the matrix will contain only the letters a and b

Example

ssdj.in

4 
maea
bcda
aaae
aaaa

ssdj.out

3

Explanation

The valid submatrices are:

ma
bc 

ea
da
ae

da
ae

Log in or sign up to be able to send submissions!