#include <iostream>
#include <algorithm>
using namespace std;
int D, W, K;
int input_array[13][20];
int min_count = 20;
void input_A(int row) {
for (int i = 0; i < W; i++)
input_array[row][i] = 0;
}
void input_B(int row) {
for (int i = 0; i < W; i++)
input_array[row][i] = 1;
}
bool test_column(int j) {
int max_count = 0;
int count = 1;
for (int i = 1; i < D; i++) {
if (input_array[i - 1][j] == input_array[i][j])
count++;
else count = 1;
if (max_count < count)
max_count = count;
}
if (max_count >= K)
return true;
}
bool performance_test() {
for (int j = 0; j < W; j++) {
if (!test_column(j))
return false;
}
return true;
}
void solve(int count, int index) {
if (count >= min_count) return;
if (performance_test()) {
min_count = count;
return;
}
if (index == D) return;
int save_array[13][20];
copy(&input_array[0][0], &input_array[0][0]+13*20, &save_array[0][0]);
solve(count, index + 1);
input_A(index);
solve(count + 1, index + 1);
copy(&save_array[0][0], &save_array[0][0]+13*20, &input_array[0][0]);
input_B(index);
solve(count + 1, index + 1);
copy(&save_array[0][0], &save_array[0][0]+13*20, &input_array[0][0]);
}
int main(int argc, char** argv)
{
int test_case;
int T;
cin >> T;
for (test_case = 1; test_case <= T; ++test_case)
{
cin >> D >> W >> K;
for (int i = 0; i < D; i++) {
for (int j = 0; j < W; j++) {
cin >> input_array[i][j];
}
}
if (K == 1) {
cout << '#' << test_case << ' ' << 0 << '\n';
continue;
}
solve(0, 0);
cout << "#" << test_case << " " << min_count << endl;
min_count = 20;
}
return 0;
}