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;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)

int n, m;
int ike[11][11];

int main() {
	for (; cin >> n >> m && n;) {
		rep(i, 0, n)rep(j, 0, m) {
			cin >> ike[i][j];
		}
		int ans = 0;
		using P = pair<int, int>;
		rep(k, 0, 10) {
			vector<set<P>> st(n);
			vector<map<P, int>> area(n);
			rep(i, 0, n) {
				int l, r;
				l = 0, r = 0;
				int a = 0;
				rep(j, 0, m) {
					if (ike[i][j] <= k) {
						r++;
						a += k + 1 - ike[i][j];
					}
					else {
						if (ike[i][l] <= k) {
							st[i].emplace(l, r);
							area[i][P(l, r)] = a;
						}
						a = 0;
						l = j + 1;
						r = j + 1;
					}
				}
				if (l != m) {
					st[i].emplace(l, r);
					area[i][P(l, r)] = a;
				}
			}
			rep(i, 1, n - 1) {
				//cout << " i " << i << endl;
				for (auto it = st[i].begin(); it != st[i].end(); it++) {
					int l, r; tie(l, r) = *it;
					P p = *it;
					if (l == 0 || r == m)continue;
					int tmp = area[i][p];
					// ???????????????
					{
						int ni = i - 1;
						if (st[ni].size() != 0) {
							auto nit = st[ni].lower_bound(p);

							// end ?????´???
							if (nit == st[ni].end()) {
								auto npit = nit; npit--;

								int nl, nr;
								tie(nl, nr) = *npit;
								if (nr < l) {
								}
								else {
									continue;
								}
							}
							else {
								P np = *nit;
								int nl, nr;
								tie(nl, nr) = np;

								if (p == np)
									continue;
								else if (nl <= r) {
									continue;
								}
								else if (r < nl) {
									if (nit == st[ni].begin()) {
									}
									else {
										auto npit = nit; npit--;
										tie(nl, nr) = *npit;
										if (nr < l) {
										}
										else {
											continue;
										}
									}
								}
							}
						}

					}
					rep(ni, i + 1, n) {
						if (st[ni].size() == 0) {
							break;
						}

						auto nit = st[ni].lower_bound(p);

						// end ?????´???
						if (nit == st[ni].end()) {
							auto npit = nit; npit--;

							int nl, nr;
							tie(nl, nr) = *npit;
							if (nr < l) {
								break;
							}
							else {
								tmp = 0;
								break;
							}
						}
						else {
							P np = *nit;
							int nl, nr;
							tie(nl, nr) = np;

							if (p == np) {
								if (ni == n - 1) {
									tmp = 0;
									break;
								}
								tmp += area[ni][np];
							}
							else if (nl <= r) {
								tmp = 0;
								break;
							}
							else if (r < nl) {
								if (nit == st[ni].begin()) {
									// ok
									break;
								}
								auto npit = nit; npit--;
								tie(nl, nr) = *npit;
								if (nr < l) {
									// ok
									break;
								}
								else {
									tmp = 0;
									break;
								}
							}
						}
					}
					ans = max(ans, tmp);
				}
			}
		}
		cout << ans << endl;
	}
}

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

int e[10][10];

int youseki(int x,int y,int dx,int dy)
{
	int height=50;
	int ret=0;
	//池の周りの高さを調べる
	for(int i=0;i<dy;i++)
	{
		for(int j=0;j<dx;j++)
		{
			if(i==0 || i==dy-1)
				height=min(height,e[y+i][x+j]);
			else if(j==0 || j==dx-1)
				height=min(height,e[y+i][x+j]);
		}
	}

	//池の中の高さを調べる
	for(int i=1;i<dy-1;i++)
	{
		for(int j=1;j<dx-1;j++)
		{
			if(height<=e[y+i][x+j])
			{
				ret=-1;
				break;
			}
			ret+=height-e[y+i][x+j];
		}
		if(ret==-1)
			break;
	}
	return ret;
}
int main()
{
	while(1)
	{
		int h,w;
		int ans=0;
		cin>>h>>w;
		if(h==0)
			break;
		
		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
				cin>>e[i][j];
		}

		for(int a=0;a<h-2;a++)
		{
			for(int b=0;b<w-2;b++)
			{
				for(int i=3;i<h-a+1;i++)
				{
					for(int j=3;j<w-b+1;j++)
						ans=max(ans,youseki(b,a,j,i));
				}
			}
		}

		cout<<ans<<endl;
	}
	return 0;
}