Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		d, w := io.NextInt(), io.NextInt()
		if d == 0 {
			break
		}
		e := make([][]int, d)
		for i := 0; i < d; i++ {
			e[i] = make([]int, w)
			for j := 0; j < w; j++ {
				e[i][j] = io.NextInt()
			}
		}
		ans := 0
		for i := 0; i < d-2; i++ {
			for j := 0; j < w-2; j++ {
				for k := i + 2; k < d; k++ {
					for l := j + 2; l < w; l++ {
						ans = intMax(vol(e, i, j, k, l), ans)
					}
				}
			}
		}
		io.Println(ans)
	}
}

func vol(e [][]int, a, b, c, d int) int {
	outer := 10
	for i := a; i <= c; i++ {
		outer = intMin(e[i][b], outer)
		outer = intMin(e[i][d], outer)
	}
	for j := b; j <= d; j++ {
		outer = intMin(e[a][j], outer)
		outer = intMin(e[c][j], outer)
	}
	a++
	b++
	vol := 0
	for i := a; i < c; i++ {
		for j := b; j < d; j++ {
			if e[i][j] >= outer {
				return 0
			}
			vol += outer - e[i][j]
		}
	}
	return vol
}

func intMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func intMin(a, b int) int {
	if a > b {
		return b
	}
	return a
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = 1 << 12;
const double EPS = 10e-8;
const double PI = 3.14159265359;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int main() {
    while (1) {
        int h, w, e[11][11];
        cin >> h >> w;
        if (h == 0) break;
        REP(i, h) {
            REP(j, w) { cin >> e[i][j]; }
        }
        int ans = 0;
        REP(it, h) {
            FOR(ib, it + 2, h) {
                REP(jl, w) {
                    FOR(jr, jl + 2, w) {
                        int hmin = 10;
                        FOR(i, it, ib+1) hmin = min(hmin, min(e[i][jl], e[i][jr]));
                        FOR(j, jl, jr+1) hmin = min(hmin, min(e[it][j], e[ib][j]));
                        bool flag = true;
                        int p = 0;
                        FOR(i, it + 1, ib && flag) {
                            FOR(j, jl + 1, jr && flag) {
                                if (e[i][j] >= hmin) {
                                    flag = false;
                                    break;
                                }
                                p += hmin - e[i][j];
                            }
                        }
                        if (flag) ans = max(ans, p);
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: //QWsin
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rep(i,x,y) for(int i=x;i<=y;++i)
using namespace std;

const int maxn=12;

int MX[maxn][maxn][maxn][maxn];
//from (i,j) to (j,k)
int mnl[maxn][maxn][maxn];
//from (i,j) up k
int mnh[maxn][maxn][maxn];
//front(i,j) right k
int sum[maxn][maxn],mx[maxn][maxn];

inline int getmin(int i,int j,int a,int b)
{
	int t=min(mnh[i][j][b-j],mnh[a][j][b-j]);
	int t2=min(mnl[a][j][a-i],mnl[a][b][a-i]);
	return min(t,t2);
}

inline int getsum(int i,int j,int a,int b)
{
	return sum[a][b]-sum[i-1][b]-sum[a][j-1]+sum[i-1][j-1];
}

int main()
{
	
	int n,m;;
	while(1)
	{
		cin>>n>>m;
		if(!n) break;
		
		rep(i,1,n) rep(j,1,m) {
			cin>>mx[i][j];	
			sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+mx[i][j];
		}
		
		rep(i,1,n) rep(j,1,m){
			mnl[i][j][0]=mx[i][j];
			rep(k,1,i-1) mnl[i][j][k]=min(mnl[i][j][k-1],mx[i-k][j]);
		}
		
		rep(i,1,n) rep(j,1,m){
			mnh[i][j][0]=mx[i][j];
			for(int k=1;j+k<=m;++k) mnh[i][j][k]=min(mnh[i][j][k-1],mx[i][j+k]);
		}
		
		rep(i,1,n) rep(j,1,m){
			MX[i][j][i][j]=mx[i][j];
			rep(a,i+1,n) MX[i][j][a][j]=max(MX[i][j][a-1][j],mx[a][j]);
			rep(a,j+1,m) MX[i][j][i][a]=max(MX[i][j][i][a-1],mx[i][a]);
			rep(a,i+1,n) rep(b,j+1,m) MX[i][j][a][b]=max(max(MX[i][j][a-1][b],MX[i][j][a][b-1]),mx[a][b]);
		}
		
		int ans=0;
		rep(i,1,n) rep(j,1,m)//zuoshang
		{
			rep(a,i+2,n) rep(b,j+2,m)//youxia
			{
				int Min=getmin(i,j,a,b);
				if(Min <= MX[i+1][j+1][a-1][b-1]) continue;
				ans=max(ans,Min*((a-i-1)*(b-j-1))-getsum(i+1,j+1,a-1,b-1));
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, a, b) for(ll i = ll(a); i < ll(b); i++)
#define rer(i, a, b) for(ll i = ll(a) - 1; i >= ll(b); i--)
#define sz(v) (int)(v).size()
#define pb push_back
#define sc second
#define fr first
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
using namespace std;
typedef long long int ll;
typedef pair <int, int> P;

const ll MOD=1000000007;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    for(;;){
        int d,w;
        cin>>d>>w;
        if(!d) return 0;
        int f[10][10];
        rep(i,0,d){
            rep(j,0,w) cin>>f[i][j];
        }
        int ans=0;
        rep(s,0,d){
            rep(t,s+3,d+1){
                rep(u,0,w){
                    rep(v,u+3,w+1){
                        int m=100;
                        rep(i,s,t){
                            rep(j,u,v){
                                if(i==s||i==t-1||j==u||j==v-1) m=min(m,f[i][j]);
                            }
                        }
                        int res=0;
                        rep(i,s,t){
                            rep(j,u,v){
                                if(!(i==s||i==t-1||j==u||j==v-1)){
                                    if(f[i][j]>=m) res=-100000000;
                                    else res+=m-f[i][j];
                                }
                            }
                        }
                        ans=max(ans,res);
                    }
                }
            }
        }
        cout <<ans<<"\n";
    }
}