Sereja and Two Lines

All submissions for this problem are available.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Sereja has a matrix A with N rows and M columns with positive integer elements. Consider the multiset S_{ij} made from the elements in the i^{th} row, along with those in the j^{th} column of A (do not count the element A_{ij} twice). Then, define a matrix F as F_{ij} = max_{x ∈ Sij} count(x, S_{ij}), where count(x, S) gives the number of times a number x occurs in the multiset S.
For example, for the matrix
1 2 1 2 3 4 1 2 1 1 1 1 2 2 2 2
The matrix F is
3 3 4 4 3 3 3 3 5 4 6 4 4 5 4 6
Your task is to find the value of the maximum element of F.
Input
The first line of input contains a single integer T, the number of test cases. T tests follow. First line of each test case contains two integers, N and M. Each of the next N lines contain M numbers, representing the matrix A.Output
For each test case, output a line containing a single integer, the answer to the problem.Constraints
 1 ≤ sum of N*M over all testcases ≤ 10^{6}
 1 ≤ A[i][j] ≤ 10^{6}
Example
Input: 1 4 4 1 2 1 2 3 4 1 2 1 1 1 1 2 2 2 2 Output: 6
Sub tasks
 Sub task #1: 1 ≤ N, M ≤ 10 (10 points)
 Sub task #2: 1 ≤ N, M ≤ 100 (20 points)
 Sub task #3: original constraints (70 points)
Author:  sereja 
Tester:  iscsi 
Editorial  http://discuss.codechef.com/problems/SEATL 
Tags  feb16, medium, sereja 
Date Added:  1122014 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, PYP3, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 