Problem list
Log in
#coci00719 · COCI 2025/2026 Contest 5

Pet

Unrated 1.0 s 512 MB

tics Frog Maša is in a lake represented by a matrix with $n$ rows and $m$ columns. The cells of the matrix are marked with 0 (water) or 1 (water lily). From one water lily, Maša can jump to any other water lily in the same row or column. If in the previous jump Maša changed the column she was in, then in the next jump she must change the row she is in. If in the previous jump Maša changed the row she was in, then in the next jump she must change the column she is in. After Maša jumps from the water lily she is currently on, it sinks and can no longer be jumped on. Maša likes to have fun and wants to visit a total of 5 water lilies along her path (including the starting water lily). Help her and calculate in how many ways she can do this if she can choose any water lily as her starting point. Two paths are considered different if the positions of their first, second, third, fourth, or fifth water lilies differ.

Input

The first line contains the natural numbers $n$ and $m$ ($1 \le n$, $m \le 2000$), as described in the problem statement. In each of the following $n$ lines there are $m$ characters, which are either 0 or 1 and represent the cells of the lake.

Output

Print a single number, the answer to the question from the problem statement.

Example 1

Input
2 3
111
110
Output
4

Example 2

Input
4 4
1111
1111
1111
1111
Output
2304

Example 3

Input
2 5
11110
01111
Output
48

Clarification of the first example: The 4 possible jump paths on water lilies are: [1, 1] →[2, 1] →[2, 2] →[1, 2] →[1, 3] [1, 2] →[2, 2] →[2, 1] →[1, 1] →[1, 3] [1, 3] →[1, 2] →[2, 2] →[2, 1] →[1, 1] [1, 3] →[1, 1] →[2, 1] →[2, 2] →[1, 2]

Source
COCI 2025/2026 Contest 5
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code