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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    while(1){
        int d, w;
        cin >> d >> w;
        if(d==0) break;
        vector<vector<int> > e(d, vector<int>(w));
        for(int i=0; i<d; i++){
            for(int j=0; j<w; j++){
                cin >> e[i][j];
            }
        }

        int ans = 0;
        for(int i=0; i<d; i++){
            for(int j=0; j<w; j++){
                for(int k=i+2; k<d; k++){
                    for(int l=j+2; l<w; l++){
                        int limit = 10;
                        for(int x=i; x<=k; x++){
                            limit = min(limit, min(e[x][j], e[x][l]));
                        }
                        for(int x=j; x<=l; x++){
                            limit = min(limit, min(e[i][x], e[k][x]));
                        }
                        int sub = 0;
                        for(int x=i+1; x<k; x++){
                            for(int y=j+1; y<l; y++){
                                if(e[x][y] >= limit){
                                    sub = 0;
                                    x = k;
                                    break;
                                }
                                sub += limit -e[x][y];
                            }
                        }
                        ans = max(ans, sub);
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll d,w;
ll field[30][30];

ll solve(ll y,ll x,ll yy,ll xx){
    ll h = INF;
    FOR(i,y,yy+1){
        FOR(j,x,xx+1){
            if(i == y || i == yy || j == x || j == xx)h = min(h,field[i][j]);
        }
    }
    ll ans = 0;
    FOR(i,y+1,yy){
        FOR(j,x+1,xx){
            if(h <= field[i][j])return 0;
            ans += h - field[i][j];
        }
    }
    return ans;
}

int main(){
    while(cin >> d >> w && d){
        rep(i,d)rep(j,w)cin >> field[i][j];
        ll ans = 0;
        rep(i,d){
            rep(j,w){
                FOR(ii,i+1,d){
                    FOR(jj,j+1,w){
                        ans = max(ans,solve(i,j,ii,jj));
                    }
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

int h, w;
int a[10][10];

int getArea(int lx, int rx, int ly, int ry) {
	int i;
	int outMin = 1145141919;
	for (i = lx; i <= rx; i++) outMin = min(outMin, a[ly][i]);
	for (i = lx; i <= rx; i++) outMin = min(outMin, a[ry][i]);
	for (i = ly; i <= ry; i++) outMin = min(outMin, a[i][lx]);
	for (i = ly; i <= ry; i++) outMin = min(outMin, a[i][rx]);
	
	int ret = 0;
	for (int y = ly + 1; y < ry; y++) {
		for (int x = lx + 1; x < rx; x++) {
			if (a[y][x] >= outMin) { return 0; }
			ret += outMin - a[y][x];
		}
	}
	return ret;
}

int main() {
	int lx, ly, rx, ry;
	
	while (cin >> h >> w) {
		if (!h) break;
		for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> a[i][j];
		
		int ans = 0;
		for (lx = 0; lx < w; lx++) {
			for (rx = lx + 2; rx < w; rx++) {
				for (ly = 0; ly < h; ly++) {
					for (ry = ly + 2; ry < h; ry++) {
						ans = max(ans, getArea(lx, rx, ly, ry));
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}