Gambling and rectangles

Compute the expected score over all equally likely rectangles, where the score squares the count of each value 1 to 5, and print it as a reduced fraction.

Hard8CombinatoricsMathPrefix sumNo attempts yetTime limit1sMemory limit256 MB

Problem

An NN by NN board is made of 1×1 square cells arranged in a square, and each cell holds one integer between 1 and 5. You want to run a gambling house with this board.

A possible board when N=4N = 4

The game works like this. First, extend every side of the board to draw N+1N+1 vertical lines and N+1N+1 horizontal lines. Then ask a customer to pick 2 distinct horizontal lines and 2 distinct vertical lines. The customer does not know where the lines are drawn, so the pick is one of the N(N+1)2×N(N+1)2\frac{N(N+1)}{2} \times \frac{N(N+1)}{2} possibilities, each equally likely.

The board has 5 vertical lines and 5 horizontal lines, and the customer picks two vertical lines and two horizontal lines

You then score the rectangle enclosed by the four chosen lines and pay the customer that amount. The scoring rule is simple. Let c1c_1, c2c_2, c3c_3, c4c_4, c5c_5 be how many 1s, 2s, 3s, 4s and 5s lie inside the rectangle. The score SS the customer receives is

S=1×c12+2×c22+3×c32+4×c42+5×c52S = 1 \times c_1^2 + 2 \times c_2^2 + 3 \times c_3^2 + 4 \times c_4^2 + 5 \times c_5^2

Because the counts are squared, the score SS grows much faster as the customer's rectangle grows. That raises the stakes and helps attract more customers. All that is left is setting the wager, and for that you need the expected value of the score SS.

Given the board, write a program that computes the expected value of SS.

Input

The first line contains an integer NN (1N10001 \le N \le 1000), the size of the board.

Each of the next NN lines contains NN integers between 1 and 5, separated by spaces. The jj-th number on the ii-th line (1iN1 \le i \le N, 1jN1 \le j \le N) is the number written in row ii from the top and column jj from the left.

Output

Print the expected value of SS on the first line as an irreducible fraction in the form p/q. Here pp and qq are integers, q1q \ge 1, and the greatest common divisor of pp and qq is 1. Print the denominator even when the expected value is an integer, so the form is p/1. For example, print 10/3 when the expected value is 103\frac{10}{3}, and 3/1 when it is 3.