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 <stdio.h>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

int IT_MAX = 1 << 18;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-9;
#define szz(x) (int)(x).size()
#define rep(i, n) for(int i=0;i<n;i++)
#define Se second
#define Fi first

int in[15][15];
int main() {
	while (1) {
		int N, M, i, j, k, l, ii, jj;
		scanf("%d %d", &N, &M);
		if (!N) break;
		for (i = 1; i <= N; i++) for (j = 1; j <= M; j++) scanf("%d", &in[i][j]);

		int ans = 0;
		for (i = 1; i <= N; i++) {
			for (j = i + 2; j <= N; j++) {
				for (k = 1; k <= M; k++) {
					for (l = k + 2; l <= M; l++) {
						int mn = INF, mx = 0;
						for (ii = i; ii <= j; ii++) {
							for (jj = k; jj <= l; jj++) {
								if (ii == i || ii == j || jj == k || jj == l) mn = min(mn, in[ii][jj]);
								else mx = max(mx, in[ii][jj]);
							}
						}
						if (mn <= mx) continue;

						int s = 0;
						for (ii = i + 1; ii < j; ii++) for (jj = k + 1; jj < l; jj++) s += mn - in[ii][jj];
						ans = max(ans, s);
					}
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define MAX 11
#define inf 1<<29
#define linf (1e16)
#define eps (1e-8)
#define Eps (1e-12)
#define mod 1000000007
#define pi acos(-1.0)
#define phi (1.0+sqrt(5.0))/2.0
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define pd(a) printf("%.10f\n",(double)(a))
#define pld(a) printf("%.10Lf\n",(ld)(a))
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)
#define Unique(v) v.erase(unique(all(v)),v.end())
#define equals(a,b) (fabs((a)-(b))<eps)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<int,double> pid;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<pii> vpi;

int n,m;
int p[MAX][MAX];

int cal(int a,int b,int c,int d){
  int h = inf,sum = 0;
  FOR(i,c,d+1)h=min(h,min(p[a][i],p[b][i]));
  FOR(i,a,b+1)h=min(h,min(p[i][c],p[i][d]));
  FOR(i,a+1,b){
    FOR(j,c+1,d){
      if(h<=p[i][j])return 0;
      sum+=(h-p[i][j]);
    }
  }
  return sum;
}

int solve(){
  int ans = 0;
  FOR(i,0,n)
    FOR(I,i+2,n)
      FOR(j,0,m)
        FOR(J,j+2,m)
          ans = max(ans,cal(i,I,j,J));
  return ans;
}

int main()
{
  while(cin>>n && cin>>m && n){
    FOR(i,0,n)
      FOR(j,0,m)cin>>p[i][j];
    cout<<solve()<<endl;
  }
  return 0;
}

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

void solve(int d, int w){
	int a[d][w];
	for(int i=0; i<d; ++i)
		for(int j=0; j<w; ++j)
			cin >> a[i][j];
	int ans = 0;
	for(int i=0; i<d; ++i)
		for(int j=0; j<w; ++j)
			for(int k=0; k<d; ++k)
				for(int l=0; l<w; ++l){
					if(i+1 >= k || j+1 >= l) continue;
					int mi = 1e9, ma = 0;
					for(int m=i; m<=k; ++m)
						for(int n=j; n<=l; ++n){
							if(m == i || m == k || n == j || n == l) mi = min(mi, a[m][n]);
							else ma = max(ma, a[m][n]);
						}
					if(mi <= ma) continue;
					int tmp = 0;
					for(int m=i; m<=k; ++m)
						for(int n=j; n<=l; ++n)
							if(m != i && m != k && n != j && n != l)
								tmp += mi - a[m][n];
					ans = max(ans, tmp);
//					cout << i << " " << j << " " << k << " " << l << " " << tmp << " " << mi << " " << ma << "\n";
				}
	cout << ans << "\n";
}

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	while(1){
		int d, w;
		cin >> d >> w;
		if(d == 0 && w == 0) return 0;
		solve(d, w);
	}
}