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 dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
 
Int INF = 1e18;
int inf = 1e9;
Int mod = 1e9+7;

int h, w;
int f[11][11];

int main() {
    vector<int> res;
    while (cin >> h >> w and h) {
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> f[i][j];
            }
        }
        int cnt = 0;
        for (int i = 3; i <= h; i++) {
            for (int j = 3; j <= w; j++) {
                for (int k = 0; k <= h - i; k++) {
                    for (int l = 0; l <= w - j; l++) {
                        // cout << k << " " << l << endl;
                        // cout << "H size:" << i << " W size:" << j << endl;
                        int mi = inf;
                        for (int m = 0; m < j; m++) {
                            mi = min(mi, f[k][l + m]);
                            mi = min(mi, f[k + i - 1][l + m]);
                        }
                        for (int m = 0; m < i; m++) {
                            mi = min(mi, f[k + m][l]);
                            mi = min(mi, f[k + m][l + j - 1]);
                        }
                        int ma = -1;
                        int sum = 0;                        
                        for (int m = 1; m < i - 1; m++) {
                            for (int n = 1; n < j - 1; n++) {
                                ma = max(ma, f[k + m][l + n]);
                                sum += mi - f[k + m][l + n];
                            }
                        }
                        // dump(ma);
                        if (mi > ma) {
                            cnt = max(cnt, sum);
                        }
                    }
                }
            }
        }
        res.push_back(cnt);
    }
    for (int i = 0; i < (int)res.size(); i++) {
        dump(res[i]);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int h,w;
int fie[11][11];
int data[11][11];
int sum[11][11];

int C(int y1,int y2,int x1,int x2,int xx){
	for(int i=x1;i<=x2;i++){
		if(data[y1][i]==1)return 0;
		if(data[y2][i]==1)return 0;
	}
	for(int i=y1;i<=y2;i++){
		if(data[i][x1]==1)return 0;
		if(data[i][x2]==1)return 0;
	}
	int res=0;
	for(int i=y1+1;i<y2;i++){
		for(int j=x1+1;j<x2;j++){
			if(data[i][j]==0)return 0;
			res+=xx-fie[i][j];
		}
	}
	return res;
}

int solve(){
	int res=0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			data[i][j]=1;
		}
	}
	for(int cc=9;cc>=1;cc--){
		for(int j=0;j<h;j++){
			for(int k=0;k<w;k++){
				data[j][k]=(fie[j][k]<cc);
			}
		}
		for(int i=0;i<h;i++){
			for(int j=i+2;j<h;j++){
				for(int k=0;k<w;k++){
					for(int l=k+2;l<w;l++){
						res=max(res,C(i,j,k,l,cc));
					}
				}
			}
		}
	}
	return res;
}

int main(void){
	while(1){
		scanf("%d%d",&h,&w);
		if(h==0 && w==0)break;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				scanf("%d",&fie[i][j]);
			}
		}
		printf("%d\n",solve());
	}
	return 0;
}

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

int main(void){
	int d, w;
	int di[] = {1,1,1,0,-1,-1,-1,0};
	int dj[] = {1,0,-1,-1,-1,0,1,1};
	while(cin >> d >> w, d+w){
		vector<int> e[d+2];
		e[0].resize(w+2); e[d+1].resize(w+2);
		for(int i = 1; i <= d; ++i){
			e[i].resize(w+2);
			for (int j = 1; j <= w; ++j){
				cin >> e[i][j];
			}
		}
		// for(int i = 0; i <= d+1; ++i){
		// 	for (int j = 0; j <= w+1; ++j){
		// 		cout << e[i][j] << " ";
		// 	}
		// 	cout << endl;
		// }
		int ans = 0;
		for(int i = 1; i <= d; ++i){
			for (int j = 1; j <= w; ++j){
				for(int k = 3; i+k <= d+1; ++k){
					for (int l = 3; j+l <= w+1; ++l){
						int m = 10;
						for(int kk = 0; kk < k; ++kk){
							m = min(m, e[i+kk][j]);
							m = min(m, e[i+kk][j+l-1]);
						}
						for(int ll = 0; ll < l; ++ll){
							m = min(m, e[i][j+ll]);
							m = min(m, e[i+k-1][j+ll]);
						}
						bool a = true;
						int tmp = 0;
						for(int kk = 1; kk < k-1; ++kk){
							for(int ll = 1; ll < l-1; ++ll){
								if(e[i+kk][j+ll]>=m) a = false;
								tmp += m-e[i+kk][j+ll];
							}
						}
						if(a) ans = max(ans, tmp);
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}