#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node
{
int x,y,z;
node *u=nullptr;
node *d=nullptr;
node *l=nullptr;
node *r=nullptr;
bool fl=false;
};
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin>>t;
for(int i=1;i<=t;i++)
{
int g,h;
cin>>g>>h;
vector<vector<node*>>a(g+2,vector<node*>(h+2,nullptr));
vector<node*>nodes;
for(int j=1;j<=g;j++)
{
for(int k=1;k<=h;k++)
{
node* n=new node();
cin>>n->z;
n->x=j;
n->y=k;
a[j][k]=n;
nodes.push_back(n);
if(j>1)
{
n->u=a[j-1][k];
a[j-1][k]->d=n;
}
}
}
for(int j=1;j<=g;j++)
{
for(int k=1;k<=h;k++)
{
if(k>1)
{
a[j][k]->l=a[j][k-1];
a[j][k-1]->r=a[j][k];
}
}
}
int ans=0;
bool flag;
do
{
flag=false;
int cnt=0;
for(auto& n:nodes)
{
if(!n->fl)
{
cnt+=n->z;
}
}
ans+=cnt;
vector<node*>dl;
for(int j=1;j<=g;j++)
{
for(int k=1;k<=h;k++)
{
node* curr=a[j][k];
if(curr->fl)
{
continue;
}
int sum = 0, count=0;
node* nei[4]={curr->u,curr->d,curr->l,curr->r};
for(node*p:nei)
{
if(p&&!p->fl)
{
sum+=p->z;
count++;
}
}
if(count>0&&curr->z*count<sum)
{
dl.push_back(curr);
}
}
}
for(node* curr:dl)
{
if(curr->fl)
{
continue;
}
curr->fl=true;
flag = true;
if(curr->u)
{
curr->u->d=curr->d;
}
if(curr->d)
{
curr->d->u=curr->u;
}
if(curr->l)
{
curr->l->r=curr->r;
}
if(curr->r)
{
curr->r->l=curr->l;
}
}
}while(flag);
for(auto& n:nodes)
{
delete n;
}
cout<<"Case #"<<i<<": "<<ans<<"\n";
}
return 0;
}
#3超时