eolymp
bolt
Try our new interface for solving problems
Problems

Vectors

Vectors

On the plane, given a set of points (\textbf{x}, \textbf{y}), where \textbf{x} and \textbf{y} -- integer, \textbf{1} ≤ \textbf{x} ≤ \textbf{M}, \textbf{1} ≤ \textbf{y} ≤ \textbf{N}. With each point out exactly one of the following vectors: (\textbf{-1}, \textbf{-1}), (\textbf{-1}, \textbf{0}), (\textbf{-1}, \textbf{1}), (\textbf{0}, \textbf{1}), (\textbf{1}, \textbf{1}), (\textbf{1}, \textbf{0}), (\textbf{1}, \textbf{-1}), (\textbf{0}, \textbf{-1}). Each vector connects one integer point of the plane with another. For example, if the point (\textbf{2},\textbf{ 5}) leaves the vector (\textbf{1},\textbf{ 1}), it connects this point with the (\textbf{3},\textbf{ 6}), but not vice versa. Sequence of two or more points in the plane \textbf{a_1}, \textbf{a_2}, \textbf{…}, \textbf{a_k} is called a cycle if each point \textbf{a_i} is joined to a vector \textbf{a_i_\{+1\}} (\textbf{1} ≤ \textbf{i }≤ \textbf{k-1}), and \textbf{a_k} is connected with the vector \textbf{a_1}. Cycles are considered different if they differ by at least one vertex. Write a program that according to the vectors coming out of the plane, finds a number of different cycles. \includegraphics{https://static.e-olymp.com/content/3d/3d36d25dbe185ed0d7a39883c6d350b0367797b0.gif} \InputFile The first line contains two integers \textbf{N} (\textbf{1} ≤ \textbf{N }≤ \textbf{100}) and \textbf{M} (\textbf{1} ≤ \textbf{M }≤ \textbf{100}). Each of the subsequent \textbf{N} lines contains \textbf{M} pairs (ie, \textbf{2×M} numbers). A pair of \textbf{x}, which is in line \textbf{y} defines a vector at (\textbf{x}, \textbf{y}). \textbf{Ouput} Print one integer - the number of cycles formed by the vectors.
Time limit 1 second
Memory limit 64 MiB
Input example #1
2 4 
-1 1 -1 1 -1 0 0 1
1 0 1 0 0 -1 0 -1
Output example #1
2
Author Shamil Yagiyayev
Source 2004 XVII All-Ukrainian Informatics Olympiad, Kharkov, March 28 - April 3, Round 1