可以发现最终结果只和 $a[1][1], a[1][2], a[2][1]$ 有关,因为所有的以 $(1, 1)$ 为左上角的矩形都至少经过 $a[1][2]$ 或者 $a[2][1]$ ,所以把 $a[1][1]$ 作为最大值,其它两个点作为最小的两个值;或者把 $a[1][1]$ 作为最小值,其它两个点作为最大的两个值。这两种情况分别计算,并不是等价的。设 $n > m$,那么最大差的个数是 $n(m - 1)$ 或者 $m(n - 1)$,因为 $n > m$,所以 $m(n - 1) > n(m - 1)$。
// Date: Wed Dec 20 21:02:01 2023
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const double eps = 1e-8;
const int dir[8][2] = {
{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1},
};
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;
const ull Pr = 131;
#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define For1(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i)
#define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i])
#define f1 first
#define f2 second
#define pb push_back
#define has(a, x) (a.find(x) != a.end())
#define nonempty(a) (!a.empty())
#define all(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
template <typename t> istream &operator>>(istream &in, vector<t> &vec) {
for (t &x : vec)
in >> x;
return in;
}
template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) {
int n = SZ(vec);
For(i, 0, n) {
out << vec[i];
if (i < n - 1)
out << ' ';
}
return out;
}
#ifdef _DEBUG
#define debug1(x) cout << #x " = " << x << endl;
#define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl;
#define debug3(x, y, z) \
cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl;
#else
#define debug1
#define debug2
#define debug3
#endif
const int N = 10010;
int t, a[N], n, m;
int main(void) {
#ifdef _DEBUG
freopen("1825b.in", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> t;
while (t--) {
cin >> n >> m;
int m1 = n * m;
For(i, 0, m1) { cin >> a[i]; }
sort(a, a + m1);
int x = a[m1 - 1], y = a[0], y1 = a[1], d1 = x - y, d2 = x - y1;
if (n < m)
swap(n, m);
ll res = d1 * (n - 1) * m + d2 * (m - 1);
x = a[0], y = a[m1 - 1], y1 = a[m1 - 2], d1 = y - x, d2 = y1 - x;
ll res1 = d1 * (n - 1) * m + d2 * (m - 1);
res = max(res, res1);
cout << res << '\n';
}
return 0;
}