#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int main() {
int t;
cin >> t;
long long arr[500][500];
long long rowS[500], colS[500];
while (t--) {
int n, m, k;
cin >> n >> m >> k;
fill(begin(rowS), end(rowS), 0);
fill(begin(colS), end(colS), 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
rowS[i] += arr[i][j];
colS[j] += arr[i][j];
}
}
long long ans = 0;
if (n <= m) {
for (int mask = 0; mask < (1 << n); mask++) {
if (__builtin_popcount(mask) > k) continue;
long long tmp = 0;
priority_queue<long long> colValues;
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) {
tmp += rowS[i];
}
}
for (int j = 0; j < m; j++) {
long long overlap = 0;
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) {
overlap += arr[i][j];
}
}
colValues.push(colS[j] - overlap);
}
int takeFromCol = k - __builtin_popcount(mask);
while (takeFromCol > 0 && !colValues.empty()) {
long long maxValue = colValues.top();
colValues.pop();
if (maxValue > 0) {
tmp += maxValue;
takeFromCol--;
}
}
ans = max(ans, tmp);
}
} else {
for (int mask = 0; mask < (1 << m); mask++) {
if (__builtin_popcount(mask) > k) continue;
long long tmp = 0;
priority_queue<long long> rowValues;
for (int i = 0; i < m; i++) {
if (mask & (1 << i)) {
tmp += colS[i];
}
}
for (int j = 0; j < n; j++) {
long long overlap = 0;
for (int i = 0; i < m; i++) {
if (mask & (1 << i)) {
overlap += arr[j][i];
}
}
rowValues.push(rowS[j] - overlap);
}
int takeFromRow = k - __builtin_popcount(mask);
while (takeFromRow > 0 && !rowValues.empty()) {
long long maxValue = rowValues.top();
rowValues.pop();
tmp += maxValue;
takeFromRow--;
}
ans = max(ans, tmp);
}
}
cout << ans << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8cXVldWU+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CiAgICBpbnQgdDsKICAgIGNpbiA+PiB0OwogICAgbG9uZyBsb25nIGFycls1MDBdWzUwMF07IAogICAgbG9uZyBsb25nIHJvd1NbNTAwXSwgY29sU1s1MDBdOyAKCiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgaW50IG4sIG0sIGs7CiAgICAgICAgY2luID4+IG4gPj4gbSA+PiBrOwogICAgICAgIAogICAgICAgIGZpbGwoYmVnaW4ocm93UyksIGVuZChyb3dTKSwgMCk7CiAgICAgICAgZmlsbChiZWdpbihjb2xTKSwgZW5kKGNvbFMpLCAwKTsKCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBtOyBqKyspIHsKICAgICAgICAgICAgICAgIGNpbiA+PiBhcnJbaV1bal07CiAgICAgICAgICAgICAgICByb3dTW2ldICs9IGFycltpXVtqXTsKICAgICAgICAgICAgICAgIGNvbFNbal0gKz0gYXJyW2ldW2pdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIAoKICAgICAgICBsb25nIGxvbmcgYW5zID0gMDsKICAgICAgICAKICAgICAgICBpZiAobiA8PSBtKSB7CiAgICAgICAgICAgIGZvciAoaW50IG1hc2sgPSAwOyBtYXNrIDwgKDEgPDwgbik7IG1hc2srKykgewogICAgICAgICAgICAgICAgaWYgKF9fYnVpbHRpbl9wb3Bjb3VudChtYXNrKSA+IGspIGNvbnRpbnVlOwoKICAgICAgICAgICAgICAgIGxvbmcgbG9uZyB0bXAgPSAwOwogICAgICAgICAgICAgICAgcHJpb3JpdHlfcXVldWU8bG9uZyBsb25nPiBjb2xWYWx1ZXM7CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKG1hc2sgJiAoMSA8PCBpKSkgewogICAgICAgICAgICAgICAgICAgICAgICB0bXAgKz0gcm93U1tpXTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBtOyBqKyspIHsKICAgICAgICAgICAgICAgICAgICBsb25nIGxvbmcgb3ZlcmxhcCA9IDA7CiAgICAgICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKG1hc2sgJiAoMSA8PCBpKSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3ZlcmxhcCArPSBhcnJbaV1bal07CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgY29sVmFsdWVzLnB1c2goY29sU1tqXSAtIG92ZXJsYXApOwogICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgIGludCB0YWtlRnJvbUNvbCA9IGsgLSBfX2J1aWx0aW5fcG9wY291bnQobWFzayk7CiAgICAgICAgICAgICAgICB3aGlsZSAodGFrZUZyb21Db2wgPiAwICYmICFjb2xWYWx1ZXMuZW1wdHkoKSkgewogICAgICAgICAgICAgICAgICAgIGxvbmcgbG9uZyBtYXhWYWx1ZSA9IGNvbFZhbHVlcy50b3AoKTsKICAgICAgICAgICAgICAgICAgICBjb2xWYWx1ZXMucG9wKCk7CiAgICAgICAgICAgICAgICAgICAgaWYgKG1heFZhbHVlID4gMCkgewogICAgICAgICAgICAgICAgICAgICAgICB0bXAgKz0gbWF4VmFsdWU7CiAgICAgICAgICAgICAgICAgICAgICAgIHRha2VGcm9tQ29sLS07CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgYW5zID0gbWF4KGFucywgdG1wKTsKICAgICAgICAgICAgfQogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGZvciAoaW50IG1hc2sgPSAwOyBtYXNrIDwgKDEgPDwgbSk7IG1hc2srKykgewogICAgICAgICAgICAgICAgaWYgKF9fYnVpbHRpbl9wb3Bjb3VudChtYXNrKSA+IGspIGNvbnRpbnVlOwoKICAgICAgICAgICAgICAgIGxvbmcgbG9uZyB0bXAgPSAwOwogICAgICAgICAgICAgICAgcHJpb3JpdHlfcXVldWU8bG9uZyBsb25nPiByb3dWYWx1ZXM7CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbTsgaSsrKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKG1hc2sgJiAoMSA8PCBpKSkgewogICAgICAgICAgICAgICAgICAgICAgICB0bXAgKz0gY29sU1tpXTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBuOyBqKyspIHsKICAgICAgICAgICAgICAgICAgICBsb25nIGxvbmcgb3ZlcmxhcCA9IDA7CiAgICAgICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtOyBpKyspIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKG1hc2sgJiAoMSA8PCBpKSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3ZlcmxhcCArPSBhcnJbal1baV07CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgcm93VmFsdWVzLnB1c2gocm93U1tqXSAtIG92ZXJsYXApOwogICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgIGludCB0YWtlRnJvbVJvdyA9IGsgLSBfX2J1aWx0aW5fcG9wY291bnQobWFzayk7CiAgICAgICAgICAgICAgICB3aGlsZSAodGFrZUZyb21Sb3cgPiAwICYmICFyb3dWYWx1ZXMuZW1wdHkoKSkgewogICAgICAgICAgICAgICAgICAgIGxvbmcgbG9uZyBtYXhWYWx1ZSA9IHJvd1ZhbHVlcy50b3AoKTsKICAgICAgICAgICAgICAgICAgICByb3dWYWx1ZXMucG9wKCk7CiAgICAgICAgICAgICAgICAgICAgdG1wICs9IG1heFZhbHVlOwogICAgICAgICAgICAgICAgICAgIHRha2VGcm9tUm93LS07CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBhbnMgPSBtYXgoYW5zLCB0bXApOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBjb3V0IDw8IGFucyA8PCBlbmRsOwogICAgfQoKICAgIHJldHVybiAwOwp9Cg==