Matrix Transformation

All submissions for this problem are available.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef has two n × m matrices A and B. He wants to make them completely identical, to achieve this goal, he can perform the following actions in a single move:
 Choose one of the matrices, either A or B.
 Choose either one row or one column of the selected matrix.
 Increment all the numbers in the selected row or column by 1.
Now Chef is wondering, what is the minimal number of moves he has to perform in order to make matrices A and B equal? Or is it just impossible?
Input
The first line of the input contains an integer T denoting the number of test cases.
For each test case, the first line of input contains two integers n and m.
The following n lines contain m space separated integers each ― the matrix A.
The next n lines contain m space separated integers each ― the matrix B.
Warning! The size of the input file can be up to 10 MB!
Output
For each test case, output a single integer ― the minimal number of moves Chef has to perform in order to make matrices A and B equal or 1 if this is not possible.Constraints
 1 ≤ T ≤ 100
 1 ≤ n ≤ m ≤ 10^{5}
 1 ≤ n × m ≤ 10^{5}
 Let us denote the sum of n × m over all T testcases by S
 1 ≤ S ≤ 5 · 10^{5}
 1 ≤ A_{ij} ≤ 10^{9}
 1 ≤ B_{ij} ≤ 10^{9}
Example
Input: 3 2 2 1 1 1 1 1 2 3 4 2 2 1 9 9 1 9 1 1 9 1 4 4 5 7 1 2 3 4 5 Output: 3 1 9
Explanation
Example case 1. We can transform the matrix A into B in three moves:1 1 > 1 2 > 1 2 > 1 2 1 1 > 1 2 > 2 3 > 3 4Example case 2. It is impossible to make these matrices equal using only the allowed moves. Example case 3. We can transform matrix A into 4 5 7 7 in six moves and matrix B into the same 4 5 7 7 in three moves.
Author:  alex_2oo8 
Tester:  kingofnumbers 
Editorial  https://discuss.codechef.com/problems/MTRNSFRM 
Tags  adhoc, alex_2oo8, cook78, easy, math, median 
Date Added:  10082016 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.5, 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, SCM chicken, 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. 