// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define mod 1000000007
#define mod2 1000000009
#define mod3 1000000021
#define pie 3.14159265359
#define mp make_pair
#define ff first
#define ss second
#define rf(i,a,b) for(ll i=a;i>=b;i--)
#define sc(a) scanf("%lld",&a)
#define pf printf
#define sz(a) (ll)(a.size())
#define psf push_front
#define ppf pop_front
#define ppb pop_back
#define pb push_back
#define pq priority_queue
#define all(s) s.begin(),s.end()
#define sp(a) setprecision(a)
#define rz resize
#define ld long double
#define inf 1e15
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define eb emplace_back
#define out fflush(stdout);
using namespace std;
int r,c;
double error=1e-9;
bool check(int i, int j)
{
if(i<=r && j<=c && i>0 && j>0)
return 1;
else
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t;
cin>>t;
f(i,1,t+1)
{
cout<<"Case #"<<i<<": ";
cin>>r>>c;
vector<vector<ll> > a(r+1,vector<ll> (c+1));
ll sum=0,ans=0;
vector<set<int> > s_row(r+2),s_col(c+2);
set<int> s3;
f(i,1,r+1)
{
s3.insert(i);
f(j,1,c+1)
{
cin>>a[i][j];
s_row[i].insert(j);
s_col[j].insert(i);
sum+=a[i][j];
}
}
f(i,1,c+1)
s_col[i].insert(r+1),s_col[i].insert(0);
f(i,1,r+1)
s_row[i].insert(c+1),s_row[i].insert(0);
bool flag=1;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
while(flag)
{
flag=0;
ans+=sum;
vector<pair<int,int> > temp,temp2;
f(i,1,r+1)
{
auto it=s_row[i].begin(),it3=s_row[i].end();
it++,it3--;
for(;it!=it3;it++)
{
int j=(*it);
double tot=0;
int cnt=0;
f(k,0,4)
{
int x,y;
auto it2=s_col[j].lb(i);
x=i,y=j;
if(dy[k]==1)
{
it++;
x=i,y=(*it);
it--;
}
else if(dy[k]==-1)
{
it--;
x=i,y=(*it);
it++;
}
if(dx[k]==1)
{
it2++;
x=(*it2),y=j;
it2--;
}
else if(dx[k]==-1)
{
it2--;
x=(*it2),y=j;
it2++;
}
if(check(x,y) && a[x][y]!=0)
{
cnt++,tot+=a[x][y];
// cout<<"Values: "<<tot<<' '<<cnt<<endl;
}
}
if(cnt!=0)
{
double val=tot/cnt;
if(val>a[i][j])
{
flag=1;
temp.eb(mp(i,j));
sum-=a[i][j];
// cout<<"Sum: "<<ans<<' '<<sum<<' '<<val<<' '<<i<<' '<<j<<"\n";
}
}
else
{
// cout<<i<<' '<<j<<endl;
temp2.eb(mp(i,j));
}
}
}
// f(i,0,sz(temp2))
// cout<<temp2[i].ff<<' '<<temp2[i].ss<<endl;
f(i,0,sz(temp))
{
// cout<<"Ent"<<endl;
s_row[temp[i].ff].erase(s_row[temp[i].ff].lb(temp[i].ss)),s_col[temp[i].ss].erase(s_col[temp[i].ss].lb(temp[i].ff));
a[temp[i].ff][temp[i].ss]=0;
if(s_row[temp[i].ff].empty())
s3.erase(temp[i].ff);
}
f(i,0,sz(temp2))
{
s_row[temp2[i].ff].erase(s_row[temp2[i].ff].lb(temp2[i].ss)),s_col[temp2[i].ss].erase(s_col[temp2[i].ss].lb(temp2[i].ff));
if(s_row[temp2[i].ff].empty())
s3.erase(temp2[i].ff);
}
}
cout<<ans<<"\n";
}
}
Ly8gI3ByYWdtYSBHQ0Mgb3B0aW1pemUoIk9mYXN0IikKLy8gI3ByYWdtYSBHQ0Mgb3B0aW1pemUgKCJ1bnJvbGwtbG9vcHMiKQovLyAjcHJhZ21hIEdDQyB0YXJnZXQoInNzZSxzc2UyLHNzZTMsc3NzZTMsc3NlNCxwb3BjbnQsYWJtLG1teCxhdngsdHVuZT1uYXRpdmUiKQojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgojZ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