COCI '25 Contest 5 #3 Pet

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem types

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 Specification

The first line contains the natural numbers n and m (1 \le n, m \le 2\,000), 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 Specification

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

Constraints

Subtask Points Constraints
1 8 n, m \le 4
2 27 n, m \le 10
3 58 n, m \le 400
4 17 No additional constraints.

Sample Input 1

2 3
111
110

Sample Output 1

4

Explanation for Sample Output 1

The 4 possible jump paths on water lilies are:

[1, 1]\to[2, 1]\to[2, 2]\to[1, 2]\to[1, 3]

[1, 2]\to[2, 2]\to[2, 1]\to[1, 1]\to[1, 3]

[1, 3]\to[1, 2]\to[2, 2]\to[2, 1]\to[1, 1]

[1, 3]\to[1, 1]\to[2, 1]\to[2, 2]\to[1, 2]

Sample Input 2

4 4
1111
1111
1111
1111

Sample Output 2

2304

Sample Input 3

2 5
11110
01111

Sample Output 3

48

Comments

There are no comments at the moment.