#K64032. Counting Unique Rectangles

    ID: 31885 Type: Default 1000ms 256MiB

Counting Unique Rectangles

Counting Unique Rectangles

You are given a binary matrix of dimensions \( m \times n \). The matrix consists of 0s and 1s. Your task is to count the number of unique rectangles that can be formed using 1s as the four corners. A rectangle is defined by four 1s at positions \((i, j)\), \((i, k)\), \((l, j)\) and \((l, k)\) where \(i < l\) and \(j < k\).

The number of rectangles that can be formed with \(k\) rows (or occurrences) containing a valid pair of ones in the same two columns is given by the formula:

\( \binom{k}{2} = \frac{k \times (k-1)}{2} \)

Read the input from standard input and output the number of unique rectangles to standard output.

inputFormat

The input is given from standard input. The first line contains two integers \(m\) and \(n\), representing the number of rows and columns in the matrix respectively. This is followed by \(m\) lines, each containing \(n\) space-separated integers (either 0 or 1), representing the rows of the matrix.

outputFormat

Output a single integer to standard output — the number of unique rectangles that can be formed using 1s as the corners.

## sample
3 3
1 0 1
1 0 1
1 0 1
3

</p>