#include <bits/stdc++.h>
using namespace std;
void get_ac(){
int E, W;
cin>>E>>W;
int x[E][W];
for(int i = 0; i < E; i++){
for(int j = 0; j < W; j++){
cin>>x[i][j];
}
}
queue<pair<int,string>> q;
q.push({0, ""});
map<pair<int,string>, int> dep;
dep[{0, ""}] = 0;
int ans = INT_MAX;
while(q.size()){
auto cur = q.front(); q.pop();
int ii = cur.first;
string str = cur.second;
// cout<<ii<<" "<<str<<" "<<dep[{ii, str}]<<"\n";
if(ii == E){
ans= min(ans, int(dep[cur] + str.length()));
continue;
}
vector<int> cnt(W, 0);
for(char c : str){
cnt[c - '0']++;
}
int next_bad = E;
for(int j = ii; j < E; j++){
bool bad = 0;
for(int k = 0; k < W; k++){
if(cnt[k] != x[j][k]){
bad = 1;
break;
}
}
if(bad) {
next_bad= j;
break;
}
}
int current_depth= dep[cur];
if(dep.find({next_bad, str}) == dep.end()){
q.push({next_bad, str}); dep[{next_bad, str}]= current_depth;
}
if(str.size() < 9){
for(int w= 0; w < W; w++){
str += (char)(w + '0');
if(dep.find({ii, str}) == dep.end()){
q.push({ii, str});
dep[{ii, str}]= current_depth + 1;
}
str.pop_back();
}
}
if(str.size()){
str.pop_back();
if(dep.find({ii, str}) == dep.end()){
q.push({ii, str});
dep[{ii, str}] = current_depth + 1;
}
}
}
cout<<ans<<"\n";
}
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
//
//
int T = 1, tc = 1;
cin>>T;
while(T--){
cout<<"Case #"<<tc++<<": ";
get_ac();
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIGdldF9hYygpewogICAgICAgIGludCBFLCBXOwogICAgICAgIGNpbj4+RT4+VzsKICAgICAgICAKICAgICAgICBpbnQgeFtFXVtXXTsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgRTsgaSsrKXsKICAgICAgICAgICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCBXOyBqKyspewogICAgICAgICAgICAgICAgICAgICAgICBjaW4+PnhbaV1bal07CiAgICAgICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHF1ZXVlPHBhaXI8aW50LHN0cmluZz4+IHE7CiAgICAgICAgcS5wdXNoKHswLCAiIn0pOwoKICAgICAgICBtYXA8cGFpcjxpbnQsc3RyaW5nPiwgaW50PiBkZXA7CiAgICAgICAgZGVwW3swLCAiIn1dID0gMDsKICAgICAgICAKICAgICAgICBpbnQgYW5zID0gSU5UX01BWDsKCiAgICAgICAgd2hpbGUocS5zaXplKCkpewogICAgICAgICAgICAgICAgYXV0byBjdXIgPSBxLmZyb250KCk7IHEucG9wKCk7CiAgICAgICAgICAgICAgICBpbnQgaWkgPSBjdXIuZmlyc3Q7CiAgICAgICAgICAgICAgICBzdHJpbmcgc3RyID0gY3VyLnNlY29uZDsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgCi8vICAgICAgICAgICAgICAgIGNvdXQ8PGlpPDwiICI8PHN0cjw8IiAiPDxkZXBbe2lpLCBzdHJ9XTw8IlxuIjsKICAgICAgICAgICAgICAgIGlmKGlpID09IEUpewogICAgICAgICAgICAgICAgICAgICAgICBhbnM9IG1pbihhbnMsIGludChkZXBbY3VyXSArIHN0ci5sZW5ndGgoKSkpOwogICAgICAgICAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHZlY3RvcjxpbnQ+IGNudChXLCAwKTsKICAgICAgICAgICAgICAgIGZvcihjaGFyIGMgOiBzdHIpewogICAgICAgICAgICAgICAgICAgICAgICBjbnRbYyAtICcwJ10rKzsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGludCBuZXh0X2JhZCA9IEU7CiAgICAgICAgICAgICAgICBmb3IoaW50IGogPSBpaTsgaiA8IEU7IGorKyl7CiAgICAgICAgICAgICAgICAgICAgICAgIGJvb2wgYmFkID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgZm9yKGludCBrID0gMDsgayA8IFc7IGsrKyl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYoY250W2tdICE9IHhbal1ba10pewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYmFkID0gMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBpZihiYWQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBuZXh0X2JhZD0gajsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaW50IGN1cnJlbnRfZGVwdGg9IGRlcFtjdXJdOwogICAgICAgICAgICAgICAgaWYoZGVwLmZpbmQoe25leHRfYmFkLCBzdHJ9KSA9PSBkZXAuZW5kKCkpewogICAgICAgICAgICAgICAgICAgICAgICBxLnB1c2goe25leHRfYmFkLCBzdHJ9KTsgZGVwW3tuZXh0X2JhZCwgc3RyfV09IGN1cnJlbnRfZGVwdGg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZihzdHIuc2l6ZSgpIDwgOSl7CiAgICAgICAgICAgICAgICAgICAgICAgIGZvcihpbnQgdz0gMDsgdyA8IFc7IHcrKyl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RyICs9IChjaGFyKSh3ICsgJzAnKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZihkZXAuZmluZCh7aWksIHN0cn0pID09IGRlcC5lbmQoKSl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBxLnB1c2goe2lpLCBzdHJ9KTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRlcFt7aWksIHN0cn1dPSBjdXJyZW50X2RlcHRoICsgMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RyLnBvcF9iYWNrKCk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmKHN0ci5zaXplKCkpewogICAgICAgICAgICAgICAgICAgICAgICBzdHIucG9wX2JhY2soKTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoZGVwLmZpbmQoe2lpLCBzdHJ9KSA9PSBkZXAuZW5kKCkpewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHEucHVzaCh7aWksIHN0cn0pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRlcFt7aWksIHN0cn1dID0gY3VycmVudF9kZXB0aCArIDE7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIGNvdXQ8PGFuczw8IlxuIjsKfQppbnQgbWFpbigpIHsKICAgICAgICBjaW4udGllKDApOwogICAgICAgIGNpbi5zeW5jX3dpdGhfc3RkaW8oMCk7Ci8vCi8vCiAgICAgICAgaW50IFQgPSAxLCB0YyA9IDE7CiAgICAgICAgY2luPj5UOwogICAgICAgIHdoaWxlKFQtLSl7CiAgICAgICAgICAgICAgICBjb3V0PDwiQ2FzZSAjIjw8dGMrKzw8IjogIjsKICAgICAgICAgICAgICAgIGdldF9hYygpOwogICAgICAgIH0KfQ==