#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int main()
{
int T;
cin >> T;
for (int test_case = 1; test_case <= T; test_case++)
{
int n, m;
cin >> n >> m;
vector<vector<int>> cost(n, vector<int>(m));
for (int r = 0; r < n; r++)
for (int c = 0; c < m; c++)
cin >> cost[r][c];
for (int r = 0; r < n; r++) sort(cost[r].begin(), cost[r].end());
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
for (int r = 0; r < n; r++)
for (int c = 0; c < m; c++)
{
int tax_increase = (c + 1) * (c + 1) - c * c;
int real_cost = cost[r][c] + tax_increase;
pq.push({ real_cost, r });
}
int cnt = 0;
int min_cost[300] = {};
while (cnt < n)
{
auto record = pq.top();
pq.pop();
int r = record.second;
while (r < n && min_cost[r]) r++;
if (r < n)
{
cnt++;
min_cost[r] = record.first;
}
}
int sum = 0;
for (int day = 0; day < n; day++)
sum += min_cost[day];
cout << "Case #" << test_case << ": " << sum << '\n';
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8cXVldWU+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpCnsKICAgIGludCBUOwogICAgY2luID4+IFQ7CgogICAgZm9yIChpbnQgdGVzdF9jYXNlID0gMTsgdGVzdF9jYXNlIDw9IFQ7IHRlc3RfY2FzZSsrKQogICAgewogICAgICAgIGludCBuLCBtOwogICAgICAgIGNpbiA+PiBuID4+IG07CgogICAgICAgIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gY29zdChuLCB2ZWN0b3I8aW50PihtKSk7CiAgICAgICAgZm9yIChpbnQgciA9IDA7IHIgPCBuOyByKyspCiAgICAgICAgICAgIGZvciAoaW50IGMgPSAwOyBjIDwgbTsgYysrKQogICAgICAgICAgICAgICAgY2luID4+IGNvc3Rbcl1bY107CgogICAgICAgIGZvciAoaW50IHIgPSAwOyByIDwgbjsgcisrKSBzb3J0KGNvc3Rbcl0uYmVnaW4oKSwgY29zdFtyXS5lbmQoKSk7CgogICAgICAgIHByaW9yaXR5X3F1ZXVlPHBhaXI8aW50LCBpbnQ+LCB2ZWN0b3I8cGFpcjxpbnQsIGludD4+LCBncmVhdGVyPHBhaXI8aW50LCBpbnQ+Pj4gcHE7CiAgICAgICAgZm9yIChpbnQgciA9IDA7IHIgPCBuOyByKyspCiAgICAgICAgICAgIGZvciAoaW50IGMgPSAwOyBjIDwgbTsgYysrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpbnQgdGF4X2luY3JlYXNlID0gKGMgKyAxKSAqIChjICsgMSkgLSBjICogYzsKICAgICAgICAgICAgICAgIGludCByZWFsX2Nvc3QgPSBjb3N0W3JdW2NdICsgdGF4X2luY3JlYXNlOwoKICAgICAgICAgICAgICAgIHBxLnB1c2goeyByZWFsX2Nvc3QsIHIgfSk7CiAgICAgICAgICAgIH0KCiAgICAgICAgaW50IGNudCA9IDA7CiAgICAgICAgaW50IG1pbl9jb3N0WzMwMF0gPSB7fTsKICAgICAgICB3aGlsZSAoY250IDwgbikKICAgICAgICB7CiAgICAgICAgICAgIGF1dG8gcmVjb3JkID0gcHEudG9wKCk7CiAgICAgICAgICAgIHBxLnBvcCgpOwoKICAgICAgICAgICAgaW50IHIgPSByZWNvcmQuc2Vjb25kOwogICAgICAgICAgICB3aGlsZSAociA8IG4gJiYgbWluX2Nvc3Rbcl0pIHIrKzsKCiAgICAgICAgICAgIGlmIChyIDwgbikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgY250Kys7CiAgICAgICAgICAgICAgICBtaW5fY29zdFtyXSA9IHJlY29yZC5maXJzdDsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgaW50IHN1bSA9IDA7CiAgICAgICAgZm9yIChpbnQgZGF5ID0gMDsgZGF5IDwgbjsgZGF5KyspCiAgICAgICAgICAgIHN1bSArPSBtaW5fY29zdFtkYXldOwoKICAgICAgICBjb3V0IDw8ICJDYXNlICMiIDw8IHRlc3RfY2FzZSA8PCAiOiAiIDw8IHN1bSA8PCAnXG4nOwogICAgfQp9