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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vd = vector<double>;
using vvd = vector<vd>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using vs = vector<string>;
#define rep(i,n) range(i,0,n)
#define range(i,a,n) for(int i=a;i<n;i++)
#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI 3.141592653589793

template<class S, class T>ostream&operator<<(ostream&os, pair<S, T>p)
{
	os << "[" << p.first << "," << p.second << "]"; return os;
}
template<class S>auto&operator<<(ostream&os, vector<S>t)
{
	bool a = 1; for (auto s : t) { os << (a ? "" : " ") << s; a = 0; }
	return os;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int h,w;
	while (cin >> h>>w, h) {
		vvi f(h, vi(w));
		vvi fsum(h+1, vi(w+1,0));
		rep(i, h)rep(j, w)cin >> f[i][j];
		int MIN[10][10][10][10];
		int MAX[10][10][10][10];
		rep(i, 10)rep(j, 10)rep(k, 10)rep(l, 10)MIN[i][j][k][l] = 0;
		rep(i, 10)rep(j, 10)rep(k, 10)rep(l, 10)MAX[i][j][k][l] = 0;
		rep(i, h)rep(j, w) {
			MIN[i][j][i][j] = f[i][j];
			MAX[i][j][i][j] = f[i][j];
			fsum[i + 1][j + 1] = f[i][j];
		}
		range(i,0, h + 1)range(j, 1, w + 1)fsum[i][j] += fsum[i][j - 1];
		range(i,1, h + 1)range(j, 0, w + 1)fsum[i][j] += fsum[i-1][j];
		//for (auto l : fsum)cout << l << endl;

		range(ylen,0,  h) rep(y, h - ylen ) {
			range(xlen,0, w ) rep(x, w - xlen) {
				if (ylen + xlen == 0)continue;
				//cout << pii(y, x) << "," << pii(y + ylen, x + xlen) << ":";
				if (ylen != 0) {
					MIN[y][x][y + ylen][x + xlen] = min(MIN[y][x][y + ylen - 1][x + xlen], MIN[y + ylen][x][y + ylen][x + xlen]);
					MAX[y][x][y + ylen][x + xlen] = max(MAX[y][x][y + ylen - 1][x + xlen], MAX[y + ylen][x][y + ylen][x + xlen]);
				}
				else {
					MIN[y][x][y + ylen][x + xlen] = min(MIN[y][x][y + ylen][x + xlen - 1], MIN[y + ylen][x + xlen][y + ylen][x + xlen]);
					MAX[y][x][y + ylen][x + xlen] = max(MAX[y][x][y + ylen][x + xlen - 1], MAX[y + ylen][x + xlen][y + ylen][x + xlen]);
				}
			}
		}
		int ans = 0;
		rep(ay, h)rep(ax, w)range(by,ay+2, h)range(bx,ax+2, w) {
			int inner = MAX[ay+1][ax+1][by-1][bx-1];
			int outer = INF;
			outer = min(outer, MIN[ay][ax][ay][bx]);
			outer = min(outer, MIN[ay][ax][by][ax]);
			outer = min(outer, MIN[ay][bx][by][bx]);
			outer = min(outer, MIN[by][ax][by][bx]);
			//cout << pii(ay, ax) << pii(by, bx) << outer << "," << inner << endl;
			if (outer > inner) {
				int high = fsum[by][bx] + fsum[ay + 1][ax + 1] - fsum[by][ax + 1] - fsum[ay + 1][bx];
				ans = max(ans, (by-ay-1)*(bx - ax - 1)*outer-high);
				//cout << pii(by, bx) << pii(ay, ax) << "=" << fsum[by][bx] + fsum[ay + 1][ax + 1] - fsum[by][ax + 1] - fsum[ay + 1][bx] << endl;
			}
		}
		cout << ans << endl;
		//cout << "=====" << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<ll,ll> Pi;
const int INF=(ll)(1LL<<31)-1;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
//int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int d,w;
int e[13][13];

int main(){
	while(1){
		cin>>d>>w;
		if(d==0&&w==0)break;
		int ans=0;
		for(int i=0;i<d;i++){
			for(int j=0;j<w;j++){
				cin>>e[i][j];
			}
		}
		for(int i=0;i<d-2;i++){
			for(int j=i+2;j<d;j++){
				for(int k=0;k<w-2;k++){
					for(int l=k+2;l<w;l++){
						int out=INF;
						for(int m=0;m<l-k;m++)
							out=min(out,e[i][m+k]);
						for(int m=0;m<l-k;m++)
							out=min(out,e[j][m+k+1]);
						for(int m=0;m<j-i;m++)
							out=min(out,e[m+i+1][k]);
						for(int m=0;m<j-i;m++)
							out=min(out,e[m+i][l]);
						int tans=0;
						for(int m=i+1;m<j;m++){
							for(int n=k+1;n<l;n++){
								if(e[m][n]>=out){
									tans=0;
									goto A;
								}
								tans+=out-e[m][n];
							}
						}
						A:
						ans=max(ans,tans);
					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}