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;

const int MAX_DW=11;

int d,w,ans,hi,lo,cnt;
vector<vector<int>> e(MAX_DW,vector<int>(MAX_DW));

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> d >> w,d){
        for (int i=0;i<d;++i)
            for (int j=0;j<w;++j)
                cin >> e[i][j];
        ans=0;
        for (int i=0;i<d;++i){
            for (int j=i+2;j<d;++j){
                for (int k=0;k<w;++k){
                    for (int l=k+2;l<w;++l){
                        hi=0,lo=10,cnt=0;
                        for (int x=i;x<=j;++x){
                            for (int y=k;y<=l;++y){
                                if (x==i||x==j||y==k||y==l){
                                    lo=min(lo,e[x][y]);
                                } else hi=max(hi,e[x][y]);
                            }
                        }
                        if (hi>=lo) continue;
                        for (int x=i+1;x<j;++x){
                            for (int y=k+1;y<l;++y){
                                cnt+=lo-e[x][y];
                            }
                        }
                        ans=max(ans,cnt);
                    }
                }
            }
        }
        cout << ans << '\n';
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1);
typedef long long ll;
typedef pair<int64_t,int64_t> pll;
int dx[]={1,-1,0,0,1,-1,1,-1};
int dy[]={0,0,1,-1,1,-1,-1,1};
#define rep(i,a,b) for (int64_t i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
//#define ALL(a) begin(a),end(a)
#define sz(s) (s).size()
#define pb push_back
//#define mp make_pair
#define fi first
#define se second
int d,w;
vector<vector<int> >e(11,vector<int>(11,0));

int judge(int y1,int y2,int x1,int x2)
{
    int maxin=0,minout=1e9,ans=0;
    for(int i=y1;i<=y2;i++)for(int j=x1;j<=x2;j++)maxin=max(maxin,e[i][j]);
    for(int i=y1-1;i<=y2+1;i++)for(int j=x1-1;j<=x2+1;j++)
    {
        if(i==y1-1||i==y2+1||j==x1-1||j==x2+1)
        {
            if(e[i][j]<=maxin)return 0;
            minout=min(minout,e[i][j]);
        }
    }
    for(int i=y1;i<=y2;i++)for(int j=x1;j<=x2;j++)ans+=minout-e[i][j];
    return ans;
}
int sepoint(int y,int x)
{
    int ans=0;
     for(int i=y;i<=d-2;i++)for(int j=x;j<=w-2;j++)ans=max(ans,judge(y,i,x,j));
    return ans;
}
int fipoint(int y,int x)
{
    int ans=0;
    rep(i,1,y-1)rep(j,1,x-1)ans=max(ans,sepoint(i,j));
    return ans;
}
int main()
{
    while(cin>>d>>w,d)
    {
        e.empty();
        REP(i,d)REP(j,w)cin>>e[i][j];
        cout<<fipoint(d,w)<<endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007;
#define INF (1<<28)
#define int long long
#define double long double
#define floot10 cout<<fixed<<setprecision(10)

signed main(){
  while(1){
    int d,w;
    cin>>d>>w;
    if(d==0&&w==0) break;
    int a[10][10];
    lp(i,d){
      lp(j,w){
	cin>>a[i][j];
      }
    }
    int ans=0;
    lp(iu,d-2){
      lp(jh,w-2){
	for(int is=iu+2;is<d;is++){
	  for(int jm=jh+2;jm<w;jm++){
	    int soto=10,naka=-1,nsum=0;
	    for(int i=iu;i<=is;i++){
	      for(int j=jh;j<=jm;j++){
		if(i==iu||i==is||j==jh||j==jm){
		  soto=min(soto,a[i][j]);
		}
		else{
		  naka=max(naka,a[i][j]);
		  nsum+=a[i][j];
		}
	      }
	    }
	    if(soto>naka){
	      ans=max(ans,(is-iu-1)*(jm-jh-1)*soto-nsum);
	    }
	  }
	}
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}