A teacher writes numbers in a square table grid with n rows and n columns. Rows and columns are counted from 0.
A pair (row r, column c) is matching when row r and column c hold the same numbers in the same order. Read the row from left to right, and the column from top to bottom.
Return the number of matching pairs. One row can match many columns, and one column can match many rows. Count every matching pair.
Examples
Example 1:
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: 0
Explanation: Row 0 is [1,2,3], but column 0 is [1,4,7]. No row is equal to any column.
Example 2:
Input: grid = [[7,7,7],[7,7,7],[7,7,7]]
Output: 9
Explanation: Every row and every column is [7,7,7]. All 3 * 3 = 9 pairs match.
Example 3:
Input: grid = [[1,3,1,3],[3,1,3,1],[1,3,1,3],[3,1,3,1]]
Output: 8
Explanation: Rows 0 and 2 are [1,3,1,3], and so are columns 0 and 2. That gives 2 * 2 = 4 pairs.
Rows 1 and 3 are [3,1,3,1], and so are columns 1 and 3. That gives 4 more pairs. The total is 8.