#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int main()
{
freopen("wigz.in", "r", stdin);
int t;
cin >> t;
int arr[500][500];
int 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];
}
}
int ans = 0;
if (n <= m)
{
for (int mask = 0; mask < (1 << n); mask++)
{
if (__builtin_popcount(mask) > k)
continue;
int tmp = 0;
multiset<int> colValues;
for (int i = 0; i < n; i++)
{
if (mask & (1 << i))
{
tmp += rowS[i];
}
}
for (int j = 0; j < m; j++)
{
int overlap = 0;
for (int i = 0; i < n; i++)
{
if (mask & (1 << i))
{
overlap += arr[i][j];
}
}
colValues.insert(colS[j] - overlap);
}
int takeFromCol = k - __builtin_popcount(mask);
while (takeFromCol > 0 && !colValues.empty())
{
auto maxValue = prev(colValues.end());
if (*maxValue > 0)
{
tmp += *maxValue;
colValues.erase(maxValue);
takeFromCol--;
}
else
{
break;
}
}
ans = max(ans, tmp);
}
}
else
{
for (int mask = 0; mask < (1 << m); mask++)
{
if (__builtin_popcount(mask) > k)
continue;
int tmp = 0;
multiset<int> rowValues;
for (int i = 0; i < m; i++)
{
if (mask & (1 << i))
{
tmp += colS[i];
}
}
for (int j = 0; j < n; j++)
{
int overlap = 0;
for (int i = 0; i < m; i++)
{
if (mask & (1 << i))
{
overlap += arr[j][i];
}
}
rowValues.insert(rowS[j] - overlap);
}
int takeFromRow = k - __builtin_popcount(mask);
while (takeFromRow > 0 && !rowValues.empty())
{
auto maxValue = prev(rowValues.end());
tmp += *maxValue;
rowValues.erase(maxValue);
takeFromRow--;
}
ans = max(ans, tmp);
}
}
cout << ans << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8c2V0PgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKCkKeyAgIAogICAgZnJlb3Blbigid2lnei5pbiIsICJyIiwgc3RkaW4pOwoKICAgIGludCB0OwogICAgY2luID4+IHQ7CiAgICBpbnQgYXJyWzUwMF1bNTAwXTsKICAgIGludCByb3dTWzUwMF0sIGNvbFNbNTAwXTsKCiAgICB3aGlsZSAodC0tKQogICAgewogICAgICAgIGludCBuLCBtLCBrOwogICAgICAgIGNpbiA+PiBuID4+IG0gPj4gazsKCiAgICAgICAgZmlsbChiZWdpbihyb3dTKSwgZW5kKHJvd1MpLCAwKTsKICAgICAgICBmaWxsKGJlZ2luKGNvbFMpLCBlbmQoY29sUyksIDApOwoKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgbTsgaisrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBjaW4gPj4gYXJyW2ldW2pdOwogICAgICAgICAgICAgICAgcm93U1tpXSArPSBhcnJbaV1bal07CiAgICAgICAgICAgICAgICBjb2xTW2pdICs9IGFycltpXVtqXTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgaW50IGFucyA9IDA7CgogICAgICAgIGlmIChuIDw9IG0pCiAgICAgICAgewogICAgICAgICAgICBmb3IgKGludCBtYXNrID0gMDsgbWFzayA8ICgxIDw8IG4pOyBtYXNrKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmIChfX2J1aWx0aW5fcG9wY291bnQobWFzaykgPiBrKQogICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlOwoKICAgICAgICAgICAgICAgIGludCB0bXAgPSAwOwogICAgICAgICAgICAgICAgbXVsdGlzZXQ8aW50PiBjb2xWYWx1ZXM7CgogICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaWYgKG1hc2sgJiAoMSA8PCBpKSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHRtcCArPSByb3dTW2ldOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG07IGorKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpbnQgb3ZlcmxhcCA9IDA7CiAgICAgICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAobWFzayAmICgxIDw8IGkpKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdmVybGFwICs9IGFycltpXVtqXTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBjb2xWYWx1ZXMuaW5zZXJ0KGNvbFNbal0gLSBvdmVybGFwKTsKICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICBpbnQgdGFrZUZyb21Db2wgPSBrIC0gX19idWlsdGluX3BvcGNvdW50KG1hc2spOwogICAgICAgICAgICAgICAgd2hpbGUgKHRha2VGcm9tQ29sID4gMCAmJiAhY29sVmFsdWVzLmVtcHR5KCkpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgYXV0byBtYXhWYWx1ZSA9IHByZXYoY29sVmFsdWVzLmVuZCgpKTsKICAgICAgICAgICAgICAgICAgICBpZiAoKm1heFZhbHVlID4gMCkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHRtcCArPSAqbWF4VmFsdWU7CiAgICAgICAgICAgICAgICAgICAgICAgIGNvbFZhbHVlcy5lcmFzZShtYXhWYWx1ZSk7IAogICAgICAgICAgICAgICAgICAgICAgICB0YWtlRnJvbUNvbC0tOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsgCiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgYW5zID0gbWF4KGFucywgdG1wKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBmb3IgKGludCBtYXNrID0gMDsgbWFzayA8ICgxIDw8IG0pOyBtYXNrKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmIChfX2J1aWx0aW5fcG9wY291bnQobWFzaykgPiBrKQogICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlOwoKICAgICAgICAgICAgICAgIGludCB0bXAgPSAwOwogICAgICAgICAgICAgICAgbXVsdGlzZXQ8aW50PiByb3dWYWx1ZXM7CgogICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtOyBpKyspCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaWYgKG1hc2sgJiAoMSA8PCBpKSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHRtcCArPSBjb2xTW2ldOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG47IGorKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpbnQgb3ZlcmxhcCA9IDA7CiAgICAgICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtOyBpKyspCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAobWFzayAmICgxIDw8IGkpKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdmVybGFwICs9IGFycltqXVtpXTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICByb3dWYWx1ZXMuaW5zZXJ0KHJvd1Nbal0gLSBvdmVybGFwKTsKICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICBpbnQgdGFrZUZyb21Sb3cgPSBrIC0gX19idWlsdGluX3BvcGNvdW50KG1hc2spOwogICAgICAgICAgICAgICAgd2hpbGUgKHRha2VGcm9tUm93ID4gMCAmJiAhcm93VmFsdWVzLmVtcHR5KCkpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgYXV0byBtYXhWYWx1ZSA9IHByZXYocm93VmFsdWVzLmVuZCgpKTsKICAgICAgICAgICAgICAgICAgICB0bXAgKz0gKm1heFZhbHVlOwogICAgICAgICAgICAgICAgICAgIHJvd1ZhbHVlcy5lcmFzZShtYXhWYWx1ZSk7IAogICAgICAgICAgICAgICAgICAgIHRha2VGcm9tUm93LS07CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBhbnMgPSBtYXgoYW5zLCB0bXApOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBjb3V0IDw8IGFucyA8PCBlbmRsOwogICAgfQoKICAgIHJldHVybiAwOwp9Cg==