Latin Squares | 프로그래밍의 벗 PivotOJ
PivotOJ

Latin Squares

시간 제한: 10000ms메모리 제한: 1024MB출처: MOOI 2019-20 finalBOJ 30699
이 문제는 본문 이미지 일부가 표시되지 않습니다. 텍스트만으로 풀이가 어려울 수 있습니다.

문제

Chris is a fan of puzzles. Recently he learned about Sudoku puzzles, that are based on Latin squares. A k×kk \times k table is called a Latin square if the number of distinct elements in the table is kk, and there are no two equal elements in the matrix that share the same row or the same column.

For example, [이미지 1], [이미지 2] and [이미지 3] are Latin squares, while [이미지 4], [이미지 5] and [이미지 6] are not.

Chris wants to make a new Latin square puzzle. However, he only has an old template, which is an n×mn \times m table. Chris wants to cut a contiguous Latin square fragment from the template. In how many ways can he do this? Two ways to cut a square are considered different if there is a cell that is present in one square, but not present in the other.

입력

The first line contains two integers nn and mm --- dimensions of the template (1n,m20001 \le n, m \le 2\,000).

The next nn lines contain strings sis_i that describe the template. Each string sis_i contains 2m2 \cdot m characters with ASCII codes between 3333 and 126126. The cell in row ii and column jj of the template contains a pair of characters si,2j1s_{i, 2 \cdot j - 1} and si,2js_{i, 2 \cdot j} (1in1 \le i \le n, 1jm1 \le j \le m). Two cells of the template contain equal elements if their ordered character pairs are equal. See the Notes section for further explanation.

출력

Print a single integer --- the number of ways to cut a Latin square from the template.

힌트

In the first sample there are 2020 ways to cut a 1×11 \times 1 Latin square, as well as 66 other ways:

[이미지 7] [이미지 8] [이미지 9]
(a) Way 1 (b) Way 2 (c) Way 3
[이미지 10] [이미지 11] [이미지 12]
(d) Way 4 (e) Way 5 (f) Way 6

예제

예제 1

입력
4 5
AABBAAAACC
BBAABBCCAA
AABBCCAABB
BBCCAABBCC
출력
26

예제 2

입력
5 10
!"#$%&'()*+,-./01234
56789:;<=>?@ABCDEFGH
IJKLMNOPQRSTUVWXYZ[\
]^_`abcdefghijklmnop
qrstuvwxyz{|}~!"#$%&
출력
50
코드를 제출하려면 로그인하세요.