#include<bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#define FOR0(i,n) for(int i=0;i<n;i++)
#define FOR(i,j,n) for(int i=j;i<n;i++)
#define FORD(i,j,k) for(int i=j;i>=k;i--)
#define FORA(i,a) for(auto &i : a)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 1000000000
#define ninf -1000000000
#define endl '\n'
#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2 source.cpp
inline long long MAX2(long long a, long long int b){return (a)>(b)?(a):(b);}
inline long long MAX3(long long a, long long b,long long c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));}
inline long long MIN2(long long a, long long b){return (a)<(b)?(a):(b);}
inline long long MIN3(long long a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<bool> vb;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<ii> vii;
typedef vector<vii> vvii;
ll const M = 1000000007LL;
struct fraction {
ll num;
ll den;
fraction(ll a, ll b) {
a %= M;
b %= M;
ll c = __gcd(a,b);
num = a/c;
den = b/c;
}
fraction operator*(fraction other) {
return fraction((num*other.num)%M,(den*other.den)%M);
}
fraction operator+(fraction other) {
return fraction(((num*other.den)%M + (den*other.num)%M)%M,(den*other.den)%M);
}
fraction operator-(fraction other) {
return fraction(((num*other.den)%M - (den*other.num)%M + M)%M,(den*other.den)%M);
}
fraction operator/(fraction other) {
return fraction((num*other.den)%M,(den*other.num)%M);
}
bool operator<(const fraction& other) const {
if (num < other.num) return true;
else if (num == other.num) return (den < other.den);
else return false;
}
};
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://x...content-available-to-author-only...i.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
size_t operator()(pair<uint64_t,uint64_t> x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x.first + FIXED_RANDOM)^(splitmix64(x.second + FIXED_RANDOM) >> 1);
}
size_t operator()(fraction x) const {
return operator()(mp(x.num,x.den));
}
};
inline ll modulo(ll a, ll m) {
return (a%m + m)%m;
}
inline ll modInverse(ll a, ll m) {
assert (__gcd(a,m) == 1);
ll m0 = m;
ll y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
ll q = a / m;
ll t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
inline ll modPow(ll x, ll y, ll m) { //x^y % m
if (y == 0) return 1LL;
else if (y == 1) return x;
else {
ll ans = modPow(x,y/2,m)%m;
if (y&1) {
return (((ans*ans)%m)*x)%m;
}
else {
return (ans*ans)%m;
}
}
}
void solve(int tc);
enum dirs{N,E,W,S};
int main() {
fast_io
ll t,c=1;
// t = 1;
cin >> t;
while (t--) {
solve(c);
c++;
}
}
void solve(int tc) {
int R,C;
cin >> R >> C;
ll dancers[R][C];
ii nebs[R][C][4];
ll interest_round(0);
ll total_interest(0);
FOR0(i,R) {
FOR0(j,C) {
cin >> dancers[i][j];
interest_round += dancers[i][j];
FOR0(k,4) nebs[i][j][k] = mp(-1,-1);
}
}
list<ii> prev;
total_interest += interest_round;
for(int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
ll sum = 0;
ll count = 0;
if (i >= 1) {
sum += dancers[i-1][j];
count++;
nebs[i][j][E] = mp(i-1,j);
}
if (i+1 <= R-1) {
sum += dancers[i+1][j];
count++;
nebs[i][j][W] = mp(i+1,j);
}
if (j >= 1) {
sum += dancers[i][j-1];
count++;
nebs[i][j][N] = mp(i,j-1);
}
if (j+1 <= C-1) {
sum += dancers[i][j+1];
count++;
nebs[i][j][S] = mp(i,j+1);
}
if (sum > count*dancers[i][j]) {
prev.pb(mp(i,j));
}
}
}
while (prev.size() != 0) {
list<ii> nex;
FORA(i,prev) {
interest_round -= dancers[i.first][i.second];
for(int j = 0; j < 4; j++) {
ii p = nebs[i.first][i.second][j];
if (p != mp(-1,-1)) {
nebs[p.first][p.second][3-j] = nebs[i.first][i.second][3-j];
}
}
for (int j = 0; j < 4; j++) {
ii p = nebs[i.first][i.second][j];
if (p != mp(-1,-1)) {
ll cnt = 0;
ll sum = 0;
for (int k = 0; k < 4; k++) {
ii p2 = nebs[p.first][p.second][k];
if (p2 != mp(-1,-1)) {
cnt++;
sum += dancers[p2.first][p2.second];
}
}
if (sum > dancers[p.first][p.second]*cnt) {
nex.pb(p);
}
}
}
}
total_interest += interest_round;
prev = nex;
}
printf("Case #%d: %lld\n",tc,total_interest);
}