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<string.h>
#include<algorithm>
using namespace std;
int n,m;
int ma[20][20];// 0~9 represents numbers; 10 represents yes; 100 represents no
int ans;
int k;
bool vis[20000];
int dx[8]={0,1,1,1,0,-1,-1,-1};
int dy[8]={-1,-1,0,1,1,1,0,-1};
bool sign;
int xl,xr,yl,yr,num,bound;
void dfs(int x,int y)
{
	if(x==1||y==1||x==n||y==m)
	{
		sign=0;
		return;
	}
	num++;
	xl=min(xl,x);
	xr=max(xr,x);
	yl=min(yl,y);
	yr=max(yr,y);
	vis[x*100+y]=1;
	int nx,ny;
	for(int i=0;i<8;i++)
	{
		nx=x+dx[i];
		ny=y+dy[i];
		if(vis[nx*100+ny])continue;
		if(ma[nx][ny]<=k)
		{
			if(nx==1||ny==1||nx==n||ny==m)
			{
				sign=0;
				return;
			}
			else
			{
				dfs(nx,ny);
				if(sign==0)return;
			}
		}
		else bound=min(bound,ma[nx][ny]);
	}
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(n==0)break;
		ans=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				scanf("%d",&ma[i][j]);
			}
		}
		for(k=0;k<=9;k++)
		{
			memset(vis,0,sizeof(vis));
			for(int i=2;i<=n-1;i++)
			{
				for(int j=2;j<=m-1;j++)
				{
					if(ma[i][j]<=k&&vis[i*100+j]==0)
					{
						sign=1;
						xl=i;xr=i;
						yl=j;yr=j;
						num=0;
						bound=10;
						dfs(i,j);
						if(sign&&(xr-xl+1)*(yr-yl+1)==num)
						{
							int sum=0;
							for(int p=xl;p<=xr;p++)
								for(int q=yl;q<=yr;q++)
									sum+=(bound-ma[p][q]);
							ans=max(ans,sum);
						}
					}
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

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;
}