Problem:
A Garden with Ponds
Mr. Gardiner is a modern garden designer who is excellent at utilizing the terrain features.
  His design method is unique: he first decides the location of ponds and design them with the terrain features intact.
According to his unique design procedure, all of his ponds are rectangular with simple aspect ratios.
  First, Mr. Gardiner draws a regular grid on the map of the garden site so that the land is divided into cells of unit square, and annotates every cell with its elevation.
  In his design method, a pond occupies a rectangular area consisting of a number of cells.
  Each of its outermost cells has to be higher than all of its inner cells.
  For instance, in the following grid map, in which numbers are elevations of cells, a pond can occupy the shaded area, where the outermost cells are shaded darker and the inner cells are shaded lighter.
  You can easily see that the elevations of the outermost cells are at least three and those of the inner ones are at most two.
A rectangular area on which a pond is built must have at least one inner cell.
  Therefore, both its width and depth are at least three.
When you pour water at an inner cell of a pond, the water can be kept in the pond until its level reaches that of the lowest outermost cells.
  If you continue pouring, the water inevitably spills over.  
  Mr. Gardiner considers the larger
capacity
the pond has, the better it is.
  Here, the capacity of a pond is the maximum amount of water it can keep.
  For instance, when a pond is built on the shaded area in the above map, its capacity is (3 − 1) + (3 − 0) + (3 − 2) = 6, where 3 is the lowest elevation of the outermost cells and 1, 0, 2 are the elevations of the inner cells.
  Your mission is to write a computer program that, given a grid map describing the elevation of each unit square cell, calculates the largest possible capacity of a pond built in the site.
Note that neither of the following rectangular areas can be a pond.
  In the left one, the cell at the bottom right corner is not higher than the inner cell.
  In the right one, the central cell is as high as the outermost cells.
Input
The input consists of at most 100 datasets, each in the following format.
d w
e
1, 1
...
e
1,
w
...
e
d
, 1
...
e
d, w
The first line contains
d
and
w
, representing the depth and the width, respectively, of the garden site described in the map. 
They are positive integers between 3 and 10, inclusive.
Each of the following
d
lines contains
w
integers between 0 and 9, inclusive, separated by a space.
The
x
-th integer in the
y
-th line of the
d
lines is the elevation of the unit square cell with coordinates (
x, y
).
The end of the input is indicated by a line containing two zeros separated by a space.
Output
For each dataset, output a single line containing the largest possible capacity of a pond that can be built in the garden site described in the dataset.
  If no ponds can be built, output a single line containing a zero.
Sample
...(truncated)

Submitted solutions:

code_snippet: #include<iostream>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#include<tuple>
#include<map>
#include<queue>
#include<set>
#include<unordered_set>
#include<stack>
#include<deque>
#include<bitset>
#include<cassert>
#include<numeric>
#include<regex>
#define ll long long
#define int_max ((int)2147483647)
#define ll_max ((ll)9223372036854775807)
#define all(a) (a).begin(), (a).end()
using namespace std;

void solve(const int d, const int w){

    vector<vector<int>> grid(d,vector<int>(w));

    for(int i=0;i<d;i++){
        for(int j=0;j<w;j++){
            cin>>grid[i][j];
        }
    }

    int ans=0;

    for(int h = 1; h < 10; h++){
        vector<vector<int>> lake(d,vector<int>(w));
        for(int i = 0; i < d; i++){
            for(int j = 0; j < w; j++){
                lake[i][j] = max(0, h - grid[i][j]);
            }
        }

        for(int i = 1; i < d-1; i++){
            for(int j = 1; j < w-1; j++){
                for(int x = i; x < d-1; x++){
                    for(int y = j; y < w-1; y++){
                        
                        int v = 0;
                        bool isLake = true;

                        for(int k = i-1; k <= x+1; k++){
                            for(int l = j-1; l <= y+1; l++){
                                if((k == i-1 || k == x+1) || (l == j-1 || l == y+1)){
                                    if(lake[k][l] != 0){
                                        isLake = false;
                                    }
                                }
                                else{
                                    if(lake[k][l] == 0){
                                        isLake = false;
                                    }
                                    else{
                                        v += lake[k][l];
                                    }
                                }
                            }
                        }

                        if(isLake){
                            ans = max(ans, v);
                        }
                    }
                }
            }
        }
    }

    cout<<ans<<endl;

}

int main(){

    int d,w;
    cin>>d>>w;

    while(0<d+w){
        solve(d,w);
        cin>>d>>w;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3380
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<long long, long long>>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;
using vvc = vector<vector<char>>;
using vvvi = vector<vector<vector<int>>>;
using pii = pair<int, int>;
using vvb = vector<vector<bool>>;
using Graph = vector<vector<ll>>;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 998244353;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); i < (int)(n); i++)
#define rrep(i, m, n) for (int i = (m); i >= (int)(n); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

vi dx4 = {1, 0, -1, 0}, dy4 = {0, 1, 0, -1};
vi dx8 = {1, 1, 1, 0, 0, -1, -1, -1}, dy8 = {1, 0, -1, 1, -1, 1, 0, -1};

int solve(vvi e, int t, int b, int l, int r)
{
    int min = INF;
    rep3(i, t, b + 1) { chmin(min, e[i][l]); }
    rep3(i, t, b + 1) { chmin(min, e[i][r]); }
    rep3(i, l, r + 1) { chmin(min, e[t][i]); }
    rep3(i, l, r + 1) { chmin(min, e[b][i]); }
    int max = 0;
    rep3(i, t + 1, b)
    {
        rep3(j, l + 1, r) { chmax(max, e[i][j]); }
    }
    if (min <= max)
    {
        return 0;
    }
    int res = 0;
    rep3(i, t + 1, b)
    {
        rep3(j, l + 1, r) { res += min - e[i][j]; }
    }
    return res;
}

int main()
{
    while (1)
    {
        // 宣言・入力の受け取り
        int d, w;
        cin >> d >> w;
        // 終了条件
        if (d == 0 && w == 0)
        {
            break;
        }
        // 宣言・入力の受け取り
        vvi e(d, vi(w));
        rep(i, d)
        {
            rep(j, w) { cin >> e[i][j]; }
        }
        // solve
        int ans = 0;
        rep(t, d)
        {
            rep(l, w)
            {
                rep(b, d)
                {
                    rep(r, w)
                    {
                        if (t + 2 <= b && l + 2 <= r)
                        {
                            chmax(ans, solve(e, t, b, l, r));
                        }
                    }
                }
            }
        }
        // 出力
        cout << ans << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3344
final_rank: 11

code_snippet: #include <bits/stdc++.h>

// #include <atcoder/all>

using namespace std;
// using namespace atcoder;
typedef long long ll;
template <class T, class... Ts>
void println(const T& a, const Ts&... b) {
    cout << a;
    (void)(cout << ... << (cout << ' ', b));
    cout << '\n';
}
template <class T>
void printv(const T& a, string sep = " ", string end = "\n") {
    for (auto x : a) {
        (void)(cout << x << sep);
    }
    cout << end;
}
void println() { cout << '\n'; }
template <class T, class... Ts>
void eprintln(const T& a, const Ts&... b) {
    cerr << a;
    (void)(cerr << ... << (cerr << ' ', b));
    cerr << '\n';
}
template <class T>
void eprintv(const T& a, string sep = " ", string end = "\n") {
    for (auto x : a) {
        (void)(cerr << x << sep);
    }
    cerr << end;
}
void eprintln() { cerr << '\n'; }
template <class... T>
void input(T&... a) { (cin >> ... >> a); }
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define yesno(a) cout << (a ? "Yes" : "No") << '\n';
#define YESNO(a) cout << (a ? "YES" : "NO") << '\n';

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cout << fixed << setprecision(15);

    while (1) {
        ll n, m;
        input(n, m);
        if (n == 0 && m == 0) break;

        vector<vector<ll>> board(n, vector<ll>(m));
        rep(i, n) rep(j, m) input(board[i][j]);

        // rep(sx, n - 2) {
        ll best_ans = 0;
        for (ll sx = 0; sx < n - 2; sx++) {
            for (ll ex = sx + 2; ex < n; ex++) {
                for (ll sy = 0; sy < m - 2; sy++) {
                    for (ll ey = sy + 2; ey < m; ey++) {
                        // println("RANGE", sx, ex, sy, ey);
                        ll maxv = 0;
                        ll minv = LLONG_MAX;
                        set<ll> outer, inner;
                        vector<ll> inner_all;
                        for (ll x = sx; x <= ex; x++) {
                            for (ll y = sy; y <= ey; y++) {
                                // println(x, y);
                                if ((x == sx || x == ex) || (y == sy || y == ey)) {
                                    // println("outer", x, y);
                                    outer.insert(board[x][y]);
                                } else {
                                    // println("inner", x, y);
                                    inner.insert(board[x][y]);
                                    inner_all.push_back(board[x][y]);
                                }
                            }
                        }
                        // printv(outer);
                        // printv(inner);
                        ll lower_outer = *outer.begin();
                        ll upper_inner = *inner.rbegin();
                        // println(lower_outer, upper_inner);
                        if (lower_outer > upper_inner) {
                            ll tot = 0;
                            for (auto i : inner_all) {
                                tot += (lower_outer - i);
                            }
                            best_ans = max(best_ans, tot);
                        }
                    }
                }
            }
        }
        println(best_ans);
    }

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3356
final_rank: 18

code_snippet: #include<bits/stdc++.h>

using namespace std;
using ll = long long;
using pint = pair<int,int>;
const int INF = 1e9;

int main()
{

	auto calc=[](vector<vector<int>> g) {
		int h=g.size();
		int w=g[0].size();
		int soto=g[0][0];
		for(int i=0;i<h;i++)soto=min({soto,g[i][0],g[i][w-1]});
		for(int i=0;i<w;i++)soto=min({soto,g[0][i],g[h-1][i]});
		
		int sum=0;
		for(int i=1;i<h-1;i++){
			for(int j=1; j<w-1;j++){
				if(g[i][j]>=soto)sum=-INF;
				else sum+=soto-g[i][j];
			}
		}
		return sum;
	};

	while(true) {
		int d,w;
		cin>>d>>w;
		if(d+w==0)break;
		vector<vector<int>> g(d,vector<int>(w));
		for(auto &i:g)for(auto &j:i)cin>>j;

		auto get_grid = [&](int y1,int x1, int y2, int x2) {
			vector<vector<int>> grid(y2-y1,vector<int>(x2-x1));
			for(int i=y1;i<y2;i++){
				for(int j=x1;j<x2;j++){
					grid[i-y1][j-x1]=g[i][j];
				}
			}
			return grid;
		};

		int ans = -INF;
		for(int y1=0;y1<d;y1++){
			for(int x1=0;x1<w;x1++){
				for(int y2=y1+3;y2<=d;y2++){
					for(int x2=x1+3;x2<=w;x2++){

						vector<vector<int>> grid=get_grid(y1,x1,y2,x2);
						int ret=calc(grid);
						ans=max(ans,ret);
					}
				}
			}
		}
		cout<<(ans<0?0:ans)<<endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3352
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
    int d, w, e[22][22], cnt[22][22], sum, ans;
    bool ike;
    while(cin >> d >> w, d!=0){
        for(int i=0;i<d;i++){
            for(int j=0;j<w;j++){
                cin >> e[i][j];
                cnt[i][j] = 0;
            }
        }
        ans = 0;
        for(int k=1;k<=9;k++){
            for(int i=0;i<d;i++){
                for(int j=0;j<w;j++){
                    if(e[i][j] < k){
                        cnt[i][j]++;
                    }
                }
            }
            for(int y1=0;y1<d;y1++){
                for(int y2=y1+2;y2<d;y2++){
                    for(int x1=0;x1<w;x1++){
                        for(int x2=x1+2;x2<w;x2++){
                            ike = true;
                            sum = 0;
                            if(count(cnt[y1]+x1, cnt[y1]+x2+1, 0) < x2-x1+1) ike = false;
                            if(count(cnt[y2]+x1, cnt[y2]+x2+1, 0) < x2-x1+1) ike = false;
                            for(int i=y1+1;i<y2;i++){
                                if(cnt[i][x1]!=0 || cnt[i][x2]!=0 || count(cnt[i]+x1+1, cnt[i]+x2, 0) > 0) ike = false;
                                sum += accumulate(cnt[i]+x1, cnt[i]+x2+1, 0);
                            }
                            if(ike){
                                ans = max(ans, sum);
                            }
                        }
                    }
                }
            }
            
        }
        cout << ans << endl;
    }
    return(0);
}
accuracy: 1
time_ms: 10
memory_kb: 3092
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;


#define rep(i, a, b)     for (auto i = (a); i < (b); ++i)
#define repc(i, a, b)    for (auto i = (a); i <= (b); ++i)
#define repr(i, a, b)    for (int i = (a); i >= (b); --i)
#define each_rev(itr, a) for (auto itr = rbegin(a); itr != rend(a); ++itr)
#define fn1(x, expr)    [&](const auto& x) { return (expr); }
#define fn2(x, y, expr) [&](const auto& x, const auto& y) { return (expr); }
#define pb          push_back
#define eb          emplace_back
#define LEN(a)      int(a.size())
#define all(a)      begin(a), end(a)
#define rall(a)     rbegin(a), rend(a)
#define lb(a, elem) distance(std::begin(a), lower_bound(all(a), (elem)))
#define ub(a, elem) distance(std::begin(a), upper_bound(all(a), (elem)))
#define MIN(a)      (*min_element(all(a)))
#define MAX(a)      (*max_element(all(a)))
#define UNIQ(a)     a.erase(unique(all(a)), end(a))
#define IN(x, begin, end) ((begin) <= (x) && (x) < (end))
// type alias
#define let const auto
using i64 = int64_t;
using u64 = uint64_t;
using i32 = int32_t;
using u32 = uint32_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using pii = pair<int, int>;
using pll = pair<i64, i64>;
using ldouble = long double;
template <class T> using vec = std::vector<T>;
template <class T> using MaxHeap = std::priority_queue<T>;
template <class T> using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
// constants
constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3f;
constexpr double PI = 3.14159265358979323846;
constexpr std::pair<int, int> DIR4[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
constexpr std::pair<int, int> DIR8[] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}};
// utils
template <class T, class U> inline bool chmin(T& a, const U& b) { return b < a && (a = b, true); }
template <class T, class U> inline bool chmax(T& a, const U& b) { return b > a && (a = b, true); }
template <class T> inline int sgn(T x) noexcept { return (T(0) < x) - (x < T(0)); }
template <class T, class U> inline auto divup(T a, U b) noexcept { return (a + b - 1) / b; }
template <class T, class U> inline auto sigma(const T& l, const U& r) noexcept { return (l + r) * (r - l + 1) / 2; }
inline i64 sigma(int l, int r) noexcept { return sigma((i64)l, (i64)r); }
inline i64 choose2(i64 n) noexcept { return n * (n - 1) >> 1; }
inline void yes(bool x = true) { cout << (x ? "yes\n" : "no\n"); }
inline void Yes(bool x = true) { cout << (x ? "Yes\n" : "No\n"); }
inline void YES(bool x = true) { cout << (x ? "YES\n" : "NO\n"); }
#define WHEN_BYTESIZE(T, bytes) template <typename T, std::enable_if_t<sizeof(T) == (bytes), std::nullptr_t> = nullptr>
WHEN_BYTESIZE(T, 4) inline int popcnt(T n) { return __builtin_popcount((unsigned)n); }
WHEN_BYTESIZE(T, 8) inline int popcnt(T n) { return __builtin_popcountll((unsigned long long)n); }
WHEN_BYTESIZE(T, 4) inline int topbit(T n) { return n == 0 ? -1 : 31 - __builtin_clz((unsigned)n); }
WHEN_BYTESIZE(T, 8) inline int topbit(T n) { return n == 0 ? -1 : 63 - __builtin_clzll((unsigned long long)n); }
WHEN_BYTESIZE(T, 4) inline int lowbit(T n) { return n == 0 ? 32 : __builtin_ctz((unsigned)n); }
WHEN_BYTESIZE(T, 8) inline int lowbit(T n) { return n == 0 ? 64 : __builtin_ctzll((unsigned long long)n); }
template <typename T> inline bool ispow2(T i) { return i && (i & -i) == i; }

template <class Container, enable_if_t<not is_same_v<typename Container::value_type, char>, nullptr_t> = nullptr>
istream& operator>>(istream& is, Container& a) {
    for (auto&& e : a) is >> e;
    return is;
}
istream& operator>>(istream& is, vector<char>& a) {
    for (auto& c: a) is >> c;
    return is;
}
template <class Container, enable_if_t<not is_same_v<typename Container::value_type, char>, nullptr_t> = nullptr>
ostream& operator<<(ostream& os, const Container& a) {
    if (&os != &cout) {
        os << '[';
        unsigned cnt = 0;
        for (auto it = begin(a); it != end(a) && cnt < 30; ++it, ++cnt) os << *it << (cnt + 1 < size(a) ? ", " : "");
        if (cnt < size(a)) os << "...";
        return os << ']';
    }
    if (a.size() == 0) return os;
    os << *begin(a);
    for (auto it = next(begin(a)); it != end(a); ++it) os << ' ' << *it;
    return os;
}
ostream& operator<<(ostream& os, const vector<char>& a) {
    os << '[';
    for (auto it = a.begin(); it != a.end(); ++it) {
        if (isprint(*it)) os << '\'' << *it << '\'';
        else os << int(*it);
        os << " "[it + 1 == a.end()];
    }
    return os << ']';
}
template <class T, class U> istream& operator>>(istream& is, pair<T, U>& p) { return is >> p.first >> p.second; }
template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { return os << '(' << p.first << ", " << p.second << ')'; }

ostream& operator<<(ostream& os, __uint128_t n) {
    if (n == 0) return os << '0';
    char buf[40]{};
    char* p = buf + 38;
    while (n) {
        *(p--) = char(n % 10) + '0';
        n /= 10;
    }
    return os << (p + 1);
}

ostream& operator<<(ostream& os, __int128_t n) {
    if (n < 0) return os << '-' << __uint128_t(-n);
    return os << __uint128_t(n);
}

istream& operator>>(istream& is, __uint128_t& n) {
    int c;
    while (isspace(c = is.get()));
    n = unsigned(c - '0');
    while (isdigit(c = is.get())) n = n * 10 + unsigned(c - '0');
    return is;
}

istream& operator>>(istream& is, __int128_t& n) {
    int c;
    bool neg;
    while (isspace(c = is.get()));
    if (c == '-') {
        n = 0;
        neg = true;
    } else {
        n = c - '0';
        neg = false;
    }
    while (isdigit(c = is.get())) n = n * 10 + (c - '0');
    if (neg) n = -n;
    return is;
}

inline void echo() { std::cout << '\n'; }

template <class Head, class... Tail>
inline void echo(Head&& head, Tail&&... tail) {
    std::cout << head;
    if constexpr (sizeof...(tail)) std::cout << ' ';
    echo(std::forward<Tail>(tail)...);
}

template <class... Args>
inline void read(Args&... args) {
    (std::cin >> ... >> args);
}

static inline void __attribute__((constructor)) io_setup() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout << std::fixed << std::setprecision(10);
}

#ifdef LOCAL_DEBUG
#define dump(...) (std::clog << "\e[34;1mL" << __LINE__ << ": \e[m"), dbg_impl::dump_(dbg_impl::split_(#__VA_ARGS__).begin(), __VA_ARGS__)
namespace dbg_impl {
vector<string> split_(const char* s) {
    vector<string> res;
    char buf[64], *end = buf;
    unsigned paren = 0, brace = 0;
    for (const char* p = s; *p != '\0'; ++p) {
        if (*p == ' ') continue;
        if (*p == ',' && paren == 0 && brace == 0) {
            res.emplace_back(buf, end), end = buf;
            continue;
        }
        if (*p == '(') ++paren;
        if (*p == ')') --paren;
        if (*p == '{') ++brace;
        if (*p == '}') --brace;
        *end++ = *p;
    }
    res.emplace_back(buf, end);
    return res;
}
void dump_(vector<string>::iterator) { std::clog << std::endl; }

template <class Head, class... Tail>
void dump_(vector<string>::iterator itr, Head&& head, Tail&&... tail) {
    std::clog << *itr << "\e[90;1m=\e[33;1m" << head << "\e[m, ";
    dump_(++itr, std::forward<Tail>(tail)...);
}
}  // namespace dbg_impl
#else
#define dump(...) ((void)0)
#endif

#define INT(...) int __VA_ARGS__; read(__VA_ARGS__)
#define LL(...)  i64 __VA_ARGS__; read(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__; read(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__; read(__VA_ARGS__)
#define VEC(type, name, len) vector<type> name(len); cin >> name

template <class T>
struct Cumsum {
    using value_type = T;
    std::valarray<T> data;
    Cumsum() = default;
    explicit Cumsum(const std::vector<T>& a) : data(a.size() + 1) {
        for (size_t i = 0; i < a.size(); ++i) data[i + 1] = data[i] + a[i];
    }
    //! range sum [l, r)
    inline const T sum(size_t l, size_t r) const {
        assert(l <= r);
        return data[r] - data[l];
    }
    inline size_t size() const { return data.size(); }
    inline auto begin() const { return std::begin(data); }
    inline auto end() const { return std::end(data); }
};

template <class T, class Func>
inline T bin_search(T ok, T ng, const Func& isOk) {
    while (std::abs(ok - ng) > 1) {
        const T mid = (ok + ng) >> 1;
        (isOk(mid) ? ok : ng) = mid;
    }
    return ok;
}

template <class ForwardItr>
auto runlength(const ForwardItr begin, const ForwardItr end) {
    vector<pair<ForwardItr, size_t>> res;
    res.reserve(distance(begin, end));
    for (auto itr = begin; itr != end;) {
        size_t cnt = 1;
        const auto head = itr++;
        while (itr != end && *itr == *head) ++itr, ++cnt;
        res.emplace_back(head, cnt);
    }
    res.shrink_to_fit();
    return res;
}

template <typename T>
constexpr T power(T x, int64_t exp) {
    T res(1);
    if (exp < 0) exp = -exp, x = 1 / x;
    for (; exp; exp >>= 1) {
        if (exp & 1) res *= x;
        x *= x;
    }
    return res;
}

template <typename CostT>
struct WeightedEdge {
    using Self = WeightedEdge<CostT>;
    uint32_t to;
    CostT cost;

    WeightedEdge() = default;
    WeightedEdge(unsigned to_, CostT cost_) : to(to_), cost(cost_) {}
    operator int() const { return (int)to; }
    operator uint32_t() const { return (uint32_t)to; }
    bool operator<(const Self& other) const { return cost < other.cost; }
    bool operator>(const Self& other) const { return cost > other.cost; }
    bool operator<=(const Self& other) const { return cost <= other.cost; }
    bool operator>=(const Self& other) const { return cost >= other.cost; }
    friend ostream& operator<<(ostream& os, const Self& e) { return os << "(to=" << e.to << ", cost=" << e.cost << ")"; }
};
template <class CostT>
using WGraph = vector<vector<WeightedEdge<CostT>>>;

using UWGraph = vector<vector<uint32_t>>;

enum class Directed : bool { No, Yes };

template <Directed directed>
UWGraph read_unweighted_graph(size_t vertex_num, size_t edge_num, bool decrement_node_id = true) {
    UWGraph g(vertex_num);
    for (size_t i = 0; i < edge_num; ++i) {
        uint32_t a, b;
        cin >> a >> b;
        if (decrement_node_id) --a, --b;
        g[a].push_back(b);
        if constexpr (directed == Directed::No) g[b].push_back(a);
    }
    return g;
}

template <Directed directed, class CostT>
WGraph<CostT> read_weighted_graph(size_t vertex_num, size_t edge_num, bool decrement_node_id = true) {
    WGraph<CostT> g(vertex_num);
    for (size_t i = 0; i < edge_num; ++i) {
        uint32_t a, b;
        CostT cost;
        cin >> a >> b >> cost;
        if (decrement_node_id) --a, --b;
        g[a].emplace_back(b, cost);
        if constexpr (directed == Directed::No) g[b].emplace_back(a, cost);
    }
    return g;
}

// -------------------- main --------------------

int main() {
  int H, W;
  while (cin >> H >> W, H && W) {
    auto S = vector(H, vector(W, 0));
    rep(i, 0, H) {
      cin >> S[i];
    }

    int ans = 0;
    rep(sy, 0, H) rep(sx, 0, W) {
      rep(gy, sy + 2, H) rep(gx, sx + 2, W) {

        vec<int> inner;
        vec<int> outer;
        repc(i, sy, gy) repc(j, sx, gx) {
          if (i == sy || i == gy || j == sx || j == gx) {
            outer.push_back(S[i][j]);
          } else {
            inner.push_back(S[i][j]);
          }
        }

        let outer_min = MIN(outer);
        let inner_max = MAX(inner);
        if (outer_min <= inner_max) continue;

        int sum = 0;
        for (let x : inner) {
          sum += outer_min - x;
        }

        chmax(ans, sum);
      }
    }

    echo(ans);
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3380
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define vi vector<int>
#define vvi vector<vector<int>>
#define vs vector<string>
#define vc vector<char>
#define pii pair<int, int>
#define us unordered_set
#define ud unordered_dict
#define um unordered_map
#define Lower_bound(vec, n) distance((vec).begin(), lower_bound((vec).begin(), (vec).end(), (n)))
#define Upper_bound(vec, n) distance((vec).begin(), upper_bound((vec).begin(), (vec).end(), (n)))
#define Erase(vec) (vec).erase(unique((vec).begin(), (vec).end()), (vec).end())
#define Find(vec, n) find((vec).begin(), (vec).end(), n)
#define rev_sort(vec) sort((vec).begin(), (vec).end(), greater<int>())
using Graph = vector<vector<int>>;
const int INF = 1LL << 60;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
//const string alphabet = abcdefghijklmnopqrstuvwxyz;
//const string Alphabet = ABCDEFGHIJKLMNOPQRSTUVWXYZ;

signed main() {
    int d, w;
    while (true) {
        cin >> d >> w;
        if (d == 0) break;

        vvi field(d, vector<int>(w));
        int intmp;
        REP(i, d) {
            REP(j, w) {
                cin >> intmp;
                field[i][j] = intmp;
            }
        }
        int ans = 0;

        FOR(height, 3, d+1) {
            FOR(width, 3, w+1) {
                REP(r_head, d+1-height) {
                    REP(c_head, w+1-width) {
                        vi out, in;
                        FOR(r, r_head, r_head+height) {
                            FOR(c, c_head, c_head+width) {
                                if ((r == r_head || c == c_head) || (r == r_head+height-1 || c == c_head+width-1)) {
                                    out.push_back(field[r][c]);
                                }
                                else {
                                    in.push_back(field[r][c]);
                                }
                            }
                        }
                        int out_min = *min_element(ALL(out));
                        int in_max = *max_element(ALL(in));
                        if (out_min <= in_max) continue;
                        int volume = out_min * in.size() - accumulate(ALL(in), 0);
                        ans = max(ans, volume);
                    }
                }
            }
        }

        cout << ans << endl;
    }

}
accuracy: 1
time_ms: 10
memory_kb: 3444
final_rank: 16

code_snippet: #include <algorithm>
#include <iomanip>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <numeric>
#include <vector>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2(i, 0, n)
#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using i64 = long long;
template<class T, class U>
bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; }
template<class T, class U>
bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; }

template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;

int main() {
    while(1) {
        int d, w;
        cin >> d >> w;
        if(d == 0 && w == 0) break;
        vector table(d, vector<int>(w));
        rep(i, d) rep(j, w) cin >> table[i][j];
        int ans = 0;
        rep(i1, d) {
            rep(j1, w){
                rep(i2, i1 + 1, d) {
                    rep(j2, j1 + 1, w) {
                        vector<int> out, in;
                        //上
                        rep(j3, j1, j2 + 1) out.push_back(table[i1][j3]);
                        //下
                        rep(j3, j1, j2 + 1) out.push_back(table[i2][j3]);
                        //左
                        rep(i3, i1, i2 + 1) out.push_back(table[i3][j1]);
                        //右
                        rep(i3, i1, i2 + 1) out.push_back(table[i3][j2]);
                        rep(i3, i1 + 1, i2) {
                            rep(j3, j1 + 1, j2) {
                                in.push_back(table[i3][j3]);
                            }
                        }
                        if(int(in.size()) == 0) continue;
                        int out_min = *min_element(all(out));
                        int in_max = *max_element(all(in));
                        if(out_min <= in_max) continue;
                        int now_ans = 0;
                        for(auto now : in) now_ans += out_min  - now;
                        chmax(ans, now_ans);
                    }
                }
            }
        }
        cout << ans << endl;
    }   
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3364
final_rank: 13

code_snippet: #include <algorithm>
#include <iomanip>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <numeric>
#include <vector>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2(i, 0, n)
#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using i64 = long long;
template<class T, class U>
bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; }
template<class T, class U>
bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; }

template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;

int main() {
    while(1) {
        int d, w;
        cin >> d >> w;
        if(d == 0 && w == 0) break;
        vector table(d, vector<int>(w));
        rep(i, d) rep(j, w) cin >> table[i][j];
        int ans = 0;
        rep(i1, d) {
            rep(j1, w){
                rep(i2, i1 + 1, d) {
                    rep(j2, j1 + 1, w) {
                        vector<int> out, in;
                        //上
                        rep(j3, j1, j2 + 1) out.push_back(table[i1][j3]);
                        // if(i1 == 0 && j1 == 0 && i2 == 2 && j2 == 3) {
                        //     cout << out << endl;
                        // }
                        //下
                        rep(j3, j1, j2 + 1) out.push_back(table[i2][j3]);
                        // if(i1 == 0 && j1 == 0 && i2 == 2 && j2 == 3) {
                        //     cout << out << endl;
                        // }
                        //左
                        rep(i3, i1, i2 + 1) out.push_back(table[i3][j1]);
                        // if(i1 == 0 && j1 == 0 && i2 == 2 && j2 == 3) {
                        //     cout << out << endl;
                        // }
                        //右
                        rep(i3, i1, i2 + 1) out.push_back(table[i3][j2]);
                        rep(i3, i1 + 1, i2) {
                            rep(j3, j1 + 1, j2) {
                                in.push_back(table[i3][j3]);
                            }
                        }
                        if(int(in.size()) == 0) continue;
                        int out_min = *min_element(all(out));
                        int in_max = *max_element(all(in));
                        if(out_min <= in_max) continue;
                        int now_ans = 0;
                        // cout << "来てる" << endl;
                        for(auto now : in) now_ans += out_min  - now;
                        // cout << "now_ans : " << now_ans << endl;
                        if(now_ans == 5) {
                            // cout << i1 << " " << j1 << " " << i2 << " " << j2 << endl;
                            // cout << "out : " << out << endl;
                            // cout << "in : " << in << endl;
                        }
                        chmax(ans, now_ans);
                    }
                }
            }
        }
        cout << ans << endl;
    }   
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3336
final_rank: 10

code_snippet: #include <bits/stdc++.h>

#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;

//using namespace boost::multiprecision;
//#include<atcoder/all>
//using namespace atcoder;

using dou =long double;
string yes="yes";
string Yes="Yes";
string YES="YES";
string no="no";
string No="No";
string NO="NO";

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

const ll mod = 998244353ll;
//const ll mod = 1000000007ll;
//const ll mod = 100000;


struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime modhttps://atcoder.jp/contests/abc166/submit?taskScreenName=abc166_f
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};

istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

#define rep(i, n)         for(ll i = 0; i < (ll)(n); i++)
#define brep(n)           for(int bit=0;bit<(1<<n);bit++)
#define bbrep(n)           for(int bbit=0;bbit<(1<<n);bbit++)
#define erep(i,container) for (auto &i : container)
#define itrep(i,container) for (auto i : container)
#define irep(i, n)        for(ll i = n-1; i >= (ll)0ll; i--)
#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)
#define reprep(i,j,h,w) rep(i,h)rep(j,w)
#define repreprep(i,j,k,h,w,n) rep(i,h)rep(j,w)rep(k,n)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];
#define pb push_back
#define pf push_front
#define query int qq;std::cin >> qq;rep(qqq,qq)
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
//#define sum(a) accumulate(all(a),0ll)
#define keta fixed<<setprecision
#define vout(a) erep(qxqxqx,a)std::cout << qxqxqx << ' ';std::cout  << std::endl;
#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))
//#define vvector(name,typ,m,n)vector<vector<typ> > name(m,vector<typ> (n))
#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));
#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));
#define case std::cout <<"Case #" <<qqq+1<<":"
#define RES(a,i,j) a.resize(i);rep(ii,i)a[ii].resize(j);
#define RESRES(a,i,j,k) a.resize(i);rep(ii,i)a[ii].resize(j);reprep(ii,jj,i,j){dp[ii][jj].resize(k)};
#define res resize
#define as assign
#define ffor for(;;)
#define ppri(a,b) std::cout << a<<" "<<b << std::endl
#define pppri(a,b,c) std::cout << a<<" "<<b <<" "<< c<<std::endl
#define ppppri(a,b,c,d) std::cout << a<<" "<<b <<" "<< c<<' '<<d<<std::endl
#define aall(x,n) (x).begin(),(x).begin()+(n)
#define SUM(a) accumulate(all(a),0ll) 
#define stirng string
#define gin(a,b) int a,b;std::cin >> a>>b;a--;b--;
#define popcount __builtin_popcount
#define permu(a) next_permutation(all(a))
#define aru(a,d) a.find(d)!=a.end()

//#define grid_input(a,type) int h,w;std::cin >> h>>w;vvector(a,type,h,w,0);reprep(i,j,h,w)std::cin >> a[i][j];

//typedef long long T;
ll ceili(ll a,ll b){
    return ((a+b-1)/b);
}
const int INF = 2'000'000'000;
const ll INF64 =32233720854775807ll;
//const ll INF64 = 9223372036854775807ll;

//const ll INF64 = 243'000'000'000'000'000'0;


//const ll MOD = 100'000'000'7ll;
const ll MOD = 998244353ll;
//const ll MOD = 1000003ll;
const ll OD = 1000000000000007ll;
const dou pi=3.141592653589793;

long long modpow(long long a, long long n) { //累乗の剰余
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

//メモ
//ゲーム(Grundy数とか)の復習をする
//リスニング力をどうにかする
//モノイドをやる！！！！
//特に理由がなければllを使おうキャンペーン
//個数制限付きナップサックの復習
//戻すDP
//周期性の解析結果の考察をする
//いい加減英語スライドを作り始める
//改良モデルを深堀りすると良さそう
//被服調査票を提出する
//PGバトル 10/24 13:00~
//学科棟まで書類を受け取りに行く
//全方位木DPとスライド最小値
//やること困ったらSRM 500番台やるといいかも

int main(){
    ffor{
    int h,w;
    std::cin >> h>>w;
    if(h==0)break;
    vvector(a,int,h,w,0);
    reprep(i,j,h,w)std::cin >> a[i][j];
    int ans=0;
    reprep(i,j,h,w){
        rrep(ii,i+2,h){
            rrep(jj,j+2,w){
                bool ok=1;
                std::vector<int> d;
                int ma=0,mi=INF;
                rrep(y,i,ii+1){
                    rrep(x,j,jj+1){
                        if(y==i||y==ii||x==j||x==jj){
                            chmin(mi,a[y][x]);
                        }
                        else{
                            chmax(ma,a[y][x]);
                            d.pb(a[y][x]);
                        }
                    }
                }
                if(mi<=ma)continue;
                int dum=0;
                erep(p,d)dum+=mi-p;
                chmax(ans,dum);
            }
        }
    }
    std::cout << ans << std::endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3188
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define N (1000000000+7)
//#define N (998244353)
#define INF 1e16
typedef long long ll;

int board[20][20];

int calc(int x,int y,int z, int w){
    int M1= 1000;
    vector<int>s;
    for(int i=x;i<=y;i++){
        for(int j=z;j<=w;j++){
            if(i==x||i==y){
                M1 = min(M1,board[i][j]);
            }
            else{
                if(j==z||j==w){
                    M1 = min(M1,board[i][j]);
                }
                else{
                    s.push_back(board[i][j]);
                }
            }
        }
    }
    sort(s.begin(),s.end(),greater<int>());
    if(M1<=s[0])return 0;
    else{
        int ans = 0;
        for(int i=0;i<s.size();i++){
            ans+=abs(M1-s[i]);
        }
        return ans;
    }

}

int main(void){
   while(true){
       int d,w;
       cin>>d>>w;
       if(d==0 && w==0)break;
       for(int i=0;i<d;i++){
           for(int j=0;j<w;j++){
               cin>>board[i][j];
           }
       }
       int ans = 0;
       for(int i=0;i<d;i++){
           for(int j=i+2;j<d;j++){
               for(int k=0;k<w;k++){
                   for(int l=k+2;l<w;l++){
                       ans = max(ans,calc(i,j,k,l));
                   }
               }
           }
       }
       cout<<ans<<endl;
   }
}
accuracy: 1
time_ms: 10
memory_kb: 3436
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long

// debug methods
// usage: debug(x,y);
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif

#define MAX_N (1000006)
#define INF (1LL << 60)
const int MOD = (int)1e9 + 7;


signed main(){
    while(1){
        int d,w;
        cin >> d >> w;
        if(d==0)break;
        vector<vector<int>> e(d, vector<int>(w));
        for(int i=0; i<d; i++)
            for(int j=0; j<w; j++)
                cin >> e[i][j];

        int ans = 0;
        for(int i=0; i<d; i++){
            for(int j=0; j<w; j++){
                // (i,j) (k,l) が池
                for(int k=i; k<d; k++){
                    for(int l=j; l<w; l++){
                        int mn = INF;
                        vector<int> inner;
                        for(int p=i; p<=k; p++){
                            for(int q=j; q<=l; q++){
                                if(p==i || p==k || q==j || q==l) {
                                    mn = min(mn, e[p][q]);
                                } else {
                                    inner.push_back(e[p][q]);
                                }
                            }
                        }
                        if(inner.empty()) continue;
                        int sum = 0;
                        bool f=true;
                        for(auto x: inner) {
                            if(mn-x<=0){
                                f=false;
                                break;
                            }
                            sum += mn-x;
                        }
                        if(f){
                            ans = max(ans, sum);
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }
}

/*

*/
accuracy: 1
time_ms: 10
memory_kb: 3152
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  for (int d, w; cin >> d >> w, d;) {
    vector<vector<int>> e(d, vector<int>(w));
    REP(i, d) REP(j, w) cin >> e[i][j];
    int ans = 0;
    REP(sx, d) REP(sy, w) {
      REP(tx, d) REP(ty, w) {
        if (sx >= tx || sy >= ty) continue;
        int mn = 1e9;
        REP(i, d) {
          if (i < sx || i > tx) continue;
          mn = min(mn, e[i][sy]);
          mn = min(mn, e[i][ty]);
        }
        REP(j, w) {
          if (j < sy || j > ty) continue;
          mn = min(mn, e[sx][j]);
          mn = min(mn, e[tx][j]);
        }
        bool f = false;
        int c = 0;
        REP(i, d) REP(j, w) {
          if (i <= sx || i >= tx) continue;
          if (j <= sy || j >= ty) continue;
          if (mn <= e[i][j]) f = true;
          c += mn - e[i][j];
        }
        if (f) continue;
        ans = max(ans, c);
      }
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3036
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(long long int i=0;i<n;++i)
typedef long long int ll;

int func(vector<vector<int>> m,int a,int b,int c,int d){
    int ret=10;
    for(int i=b;i<=d;i++){
        ret=min({ret,m[a][i],m[c][i]});
    }
    for(int i=a;i<=c;i++){
        ret=min({ret,m[i][b],m[i][d]});
    }
    int ff=-1;
    a++;b++;c--;d--;
    for(int i=b;i<=d;i++){
        ff=max({ff,m[a][i],m[c][i]});
    }
    for(int i=a;i<=c;i++){
        ff=max({ff,m[i][b],m[i][d]});
    }
    if(ff>=ret){
        ret=-1;
    }
    return ret;
}

int main(){

    while(1){
        int d,w;
        cin >> d >> w;
        if(d==0)break;
        vector<vector<int>> m(d,vector<int>(w));
        for(int i=0;i<d;i++){
            for(int j=0;j<w;j++){
                cin >> m[i][j];
            }
        }
        int ans=0;
        for(int i=0;i<=d-3;i++){
            for(int j=0;j<=w-3;j++){
                for(int k=i+2;k<d;k++){
                    for(int l=j+2;l<w;l++){
                        int mi=func(m,i,j,k,l);
                        int su=0;
                        if(mi==-1)continue;
                        for(int s=i+1;s<k;s++){
                            for(int t=j+1;t<l;t++){
                                su+=max(0,mi-m[s][t]);
                            }
                        }
                        ans=max(ans,su);
                    }
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3060
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;



int main(){

while(1){
    int d,w;
    cin >> d >>w;
    if(d==0 && w==0)break;
    int ground[d][w];
    for(int i=0;i<d;i++){
        for(int j=0;j<w;j++){
            cin >>ground[i][j];
        }
    }
    int ans=0;
    vector<int> lake(0);
    vector<int> wall(0);
    for(int dstart=0;dstart<d-2;dstart++){
        for(int dend=dstart+2;dend<d;dend++){
            for(int wstart=0;wstart<w-2;wstart++){
                for(int wend=wstart+2;wend<w;wend++){
                    lake.clear();
                    wall.clear();
                    for(int dd=dstart;dd<=dend;dd++){
                        for(int ww=wstart;ww<=wend;ww++){
                            if(dd==dstart || dd==dend || ww==wstart|| ww==wend){
                                wall.push_back(ground[dd][ww]);
                            }
                            else lake.push_back(ground[dd][ww]);
                        }
                    }
                    if(wall.size()!=0)sort(wall.begin(),wall.end());
                    if(lake.size()!=0){
                        sort(lake.begin(),lake.end());
                        reverse(lake.begin(),lake.end());
                        }
                    int temp=0;
                    for(int pl=0;pl<lake.size();pl++)temp+=(wall[0]-lake[pl]);
                    if(wall[0]<=lake[0])temp=0;
                    ans=max(ans,temp);
                }
            }
        }
    }
    cout << ans <<endl;
    }

}
accuracy: 1
time_ms: 10
memory_kb: 3100
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

int solve(int d, int w) {
    int e[d][w];
    for (int i = 0; i < d; i++) {
        for (int j = 0; j < w; j++) {
            cin >> e[i][j];
        }
    }
    int ans = 0;
    for (int x1 = 0; x1 < d; x1++) {
        for (int x2 = 0; x2 < d; x2++) {
            if (x2 - x1 < 2) continue;
            for (int y1 = 0; y1 < w; y1++) {
                for (int y2 = 0; y2 < w; y2++) {
                    if (y2 - y1 < 2) continue;
                    int inner_height = 0;
                    int surround_height = 100;
                    int inner_sum = 0;
                    int counter = 0;
                    for (int i = 0; i < d; i++) {
                        for (int j = 0; j < w; j++) {
                            if (x1 < i && i < x2 && y1 < j && j < y2) {
                                inner_height = max(inner_height, e[i][j]);
                                inner_sum += e[i][j];
                                counter++;
                            }
                            if (x1 <= i && i <= x2 && (j == y1 || j == y2)) {
                                surround_height = min(surround_height, e[i][j]);
                            }
                            if (y1 <= j && j <= y2 && (i == x1 || i == x2)) {
                                surround_height = min(surround_height, e[i][j]);
                            }
                        }
                    }
                    if (surround_height > inner_height) {
                        int tmp = counter * surround_height - inner_sum;
                        ans = max(ans, tmp);
                    }
                }
            }
        }
    }
    return ans;
}

int main() {
    int d, w;
    while (true) {
        cin >> d >> w;
        if (!d && !w) return 0;
        cout << solve(d, w) << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3024
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using P = pair<ll, ll>;

const ll INF = 5e15;

int main(){
    while(1){
        ll H, W;
        ll ans = 0;
        cin >> H >> W;
        if(!H && !W) break;
        vector<vector<ll>> V(H, vector<ll>(W));
        for(auto &v : V) for(auto &e : v) cin >> e;
        for(ll up = 0; up < H; up++){
            for(ll down = up + 2; down < H; down++){
                for(ll left = 0; left < W; left++){
                    for(ll right = left + 2; right < W; right++){
                        ll mine = INF, maxl = 0;
                        ll suml = 0;
                        for(ll h = 0; h < H; h++){
                            for(ll w = 0; w < W; w++){
                                if(!(left <= w && w <= right &&
                                     up   <= h && h <= down)) continue;
                                if(h == up   || 
                                   h == down ||
                                   w == left || 
                                   w == right){
                                    mine = min(mine, V[h][w]);
                                }else{
                                    maxl = max(maxl, V[h][w]);
                                    suml += V[h][w];
                                }
                            }
                        }
                        if(mine <= maxl) continue;
                        ll lenh = down - up + 1;
                        ll lenw = right - left + 1;
                        ll area = lenh * lenw - 2 * lenh - 2 * lenw + 4;
                        ans = max(ans, area * mine - suml);
                    }
                }
            }
        }
        cout << ans << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3156
final_rank: 8

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

struct Pond
{
  int up,down,left,right;
  vector<int> canal;
  vector<int> moat;
  int min,max;
  void make(int up,int down,int left,int right,vector<int> canal,vector<int>moat)
  {
    this->up=up; this->down=down; this->left=left; this->right=right;
    this->canal=canal; this->moat=moat;
  }
  void setVals(){
    max= *max_element(canal.begin(),canal.end());
    min= *min_element(moat.begin(),moat.end());
  }
  bool isMake()
  {
    return (max<min);
  }
};

int calc(Pond pond)
{
  int sum=0;
  for(int i=0;i<pond.canal.size();i++){
    sum+=pond.min-pond.canal[i];
  }
  return sum;
}
void print(vector<int> list){
  for(int x:list) cout<<x<<" ";
  cout<<endl;
}
int main()
{
  while(1){
    int depth,width;
    vector<int> canal(0),moat(0);
    cin>>depth>>width;
    int stage[depth][width];
    if((depth==0)and(width==0)) return 0;
  
    for(int i=0;i<depth;i++){
      for(int j=0;j<width;j++){
	cin>>stage[i][j];
      }
    }
    int mx=0;
    for(int up=0;up<depth-1;up++){
      for(int left=0;left<width-1;left++){
	for(int down=up+2;down<depth;down++){
	  for(int right=left+2;right<width;right++){
	    //cout<<"up="<<up<<" down="<<down<<endl;
	    //cout<<"left="<<left<<" right="<<right<<endl;
	    canal.resize(0);
	    moat.resize(0);
	    for(int s=up;s<=down;s++){
	      for(int t=left;t<=right;t++){
		//cout<<"s="<<s<<" t="<<t<<endl;
		if((s!=up and s!=down)and(t!=left and t!=right)){
		  //cout<<"canal.push("<<stage[s][t]<<")"<<endl;
		  canal.push_back(stage[s][t]);
		}else{
		  //cout<<"moat.push("<<stage[s][t]<<")"<<endl;
		  moat.push_back(stage[s][t]);
		}
	      }
	    }
	    //print(canal);
	    //print(moat);
	    Pond pond;
	    pond.make(up,down,left,right,canal,moat);
	    pond.setVals();
	    //cout<<"make pond:"<<endl;
	    if(pond.isMake()){
	      //cout<<"judge=YES"<<endl;
	      mx=max(mx,calc(pond));
	    }else{
	      //cout<<"judge=NO"<<endl;
	    }
	  }
	}
      }
    }
    //cout<<"count="<<count<<endl;
    cout<<mx<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3140
final_rank: 6