The problem is from the ACM Collegiate Programming Contest: 5924 - Parallelogram Counting
The International Consortium for Parallelogram Counting (ICPC) wants to move into the digital age and automate their parallelogram counting. They want to count the number of parallelograms formed by universities in various countries. For each country, they have a map which is divided into a regular n × n grid. In each cell of the grid, they have indicated whether a university is present or not (there are never two universities in the same grid cell).
After some argument, they have settled on the following definition of a parallelogram: it is a set of four distinct points (A, B, C, D), such that AB and CD are parallel and have the same length. Note that a parallelogram can have zero area. Two parallelograms are considered different if one contains a point that the other does not; simply considering the same four points in a different order does not make a new parallelogram.
Input consists of multiple test cases, each describing a country.
Each case begins with a line containing the integer n (1 ≤ n ≤ 500), the size of the grid. This is followed by n lines of n characters. Each character is either a ‘1’ to indicate that a university is present, or a ‘0’ to indicate that no university is present, in the corresponding grid cell. There are no spaces between the characters.
The last case is followed by a line containing ‘-1’, equivalent to n == −1. There will be at most three test cases in which n > 20.
For each country, output a line containing the number of parallelograms formed by universities in that country. Note that this number might be large.
I've been able to parse the input and create an array for each country, but I can't seem to understand how they calculate the number of parallelograms formed.