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 MBAn N by N 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=4
The game works like this. First, extend every side of the board to draw N+1 vertical lines and N+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 2N(N+1)×2N(N+1) 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 c1, c2, c3, c4, c5 be how many 1s, 2s, 3s, 4s and 5s lie inside the rectangle. The score S the customer receives is
S=1×c12+2×c22+3×c32+4×c42+5×c52
Because the counts are squared, the score S 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 S.
Given the board, write a program that computes the expected value of S.
The first line contains an integer N (1≤N≤1000), the size of the board.
Each of the next N lines contains N integers between 1 and 5, separated by spaces. The j-th number on the i-th line (1≤i≤N, 1≤j≤N) is the number written in row i from the top and column j from the left.
Print the expected value of S on the first line as an irreducible fraction in the form p/q. Here p and q are integers, q≥1, and the greatest common divisor of p and q 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 310, and 3/1 when it is 3.