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 <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;



int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	while (1) {
		ll H, W; scanf("%lld %lld", &H, &W);
		if (H == 0 && W == 0) break;
		ll res = 0;
		vector<vector<ll>> vs(H, vector<ll>(W)); for (ll i = 0; i < H; i++) for (ll j = 0; j < W; j++) scanf("%lld", &vs[i][j]);
		for (ll i = 1; i < H - 1; i++) {
			for (ll j = 1; j < W - 1; j++) {
				for (ll k = i + 1; k < H; k++) {
					for (ll l = j + 1; l < W; l++) {
						ll mx = 0;
						ll mn = INF;
						ll tmp = 0;
						for (ll m = i - 1; m <= k; m++) {
							chmin(mn, vs[m][j - 1]);
							chmin(mn, vs[m][l]);
						}
						for (ll n = j - 1; n <= l; n++) {
							chmin(mn, vs[i - 1][n]);
							chmin(mn, vs[k][n]);
						}
						for (ll m = i; m < k; m++) {
							for (ll n = j; n < l; n++) {
								chmax(mx, vs[m][n]);
								tmp += (mn - vs[m][n]);
							}
						}
						//cout << "a: " << mn << " " << mx << endl;
						if (mx < mn) {
							chmax(res, tmp);
						}

					}
				}
			}
		}
		printf("%lld\n", res);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;

int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};

int main(){
    int d, w;
    while(cin>>d>>w, d){
        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 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++){

                        int nmin=10, nmax=0;
                        for(int y=i; y<=j; y++){
                            for(int x=k; x<=l; x++){
                                if(y==i || y==j || x==k || x==l){
                                    chmin(nmin, e[y][x]);
                                }
                                else{
                                    chmax(nmax, e[y][x]);
                                }
                            }
                        }

                        int sum=0;
                        if(nmax<nmin){
                            for(int y=i; y<=j; y++){
                                for(int x=k; x<=l; x++){
                                    if(y==i || y==j || x==k || x==l){
                                        continue;
                                    }
                                    sum+=nmin-e[y][x];
                                }
                            }
                        }

                        chmax(ans, sum);
                    }
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#define int long long
#define mod 1000000007
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
int h, w, map[20][20], ans;
int f(int i, int j, int k, int l) {
	int a = 9, b = 0;
	for (int p = i; p <= j; p++) {
		for (int q = k; q <= l; q++) {
			if (p == i || p == j || q == k || q == l)a = min(a, map[p][q]);
			else b = max(b, map[p][q]);
		}
	}
	if (b >= a)return 0;
	int r = 0;
	for (int p = i + 1; p < j; p++) {
		for (int q = k + 1; q < l; q++) {
			r += a - map[p][q];
		}
	}
	return r;
}
signed main() {
	while (cin >> h >> w, h) {
		for1(i, h)for1(j, w)cin >> map[i][j];
		ans = 0;
		for1(i, h)for (int j = i + 2; j <= h; j++) {
			for1(k, w)for (int l = k + 2; l <= w; l++) {
				ans = max(ans, f(i, j, k, l));
			}
		}
		cout << ans << endl;
	}
}