Boolean Product of Boolean Matrices

Time limit2sMemory limit512 MB

Summary
Compute the Boolean product of two N by N 0/1 matrices and count how many entries in the result are 1.
Level

Medium4 of 10

Topics
Matrix, Brute force, Bit manipulation
Solved
No attempts yet

Problem

While setting problems, Ukje suddenly wanted to write a twisted one. Unfortunately, many contestants in this contest are new to programming, so he could not. Still, Ukje could not give up his urge to torment the freshmen.

"Ha ha! Can the freshmen really solve this one?"

The task is simple. You are given two N×NN \times N Boolean matrices (matrices made only of 0s and 1s) A=[aij]A=[a_{ij}] and B=[bij]B=[b_{ij}]. Compute their Boolean product C=[cij]C=[c_{ij}] and count the 1s in CC. The Boolean product is computed as follows.

cij=(ai1∧b1j)∨(ai2∧b2j)∨⋯∨(ain∧bnj)c_{ij} = (a_{i1} \land b_{1j}) \lor (a_{i2} \land b_{2j}) \lor \cdots \lor (a_{in} \land b_{nj})

Here xijx_{ij} is the element in row ii, column jj of matrix XX, ∧\land is logical AND, and ∨\lor is logical OR. Now, start coding!

Input

The first line contains the matrix size NN (1≤N≤3001 \le N \le 300). The next NN lines contain the Boolean matrix AA, and the NN lines after that contain the Boolean matrix BB. Each of these lines contains NN integers, each 0 or 1, separated by spaces.

Output

Print the number of 1s in the matrix CC, the Boolean product of AA and BB.

Hint

The Boolean product for sample 1 is:

1 1 1
1 1 1
0 0 1

So the number of 1s is 7.

The Boolean product for sample 2 is:

0 0
1 1

So the number of 1s is 2.

Examples2

  1. Example 1

    Input
    3
    1 1 0
    0 1 0
    0 0 1
    1 0 0
    1 1 1
    0 0 1
    
    Expected output
    7
    
  2. Example 2

    Input
    2
    0 1
    1 1
    1 1
    0 0
    
    Expected output
    2