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>
#define ll long long
#define fi first
#define se second
#define rep(i, j, k) for (auto i = j; i < k; i++)
#define rrep(i, j, k) for (auto i = j; i > k; i--)
using namespace std;

void solve(int &h, int &w) {
  int a[11][11];
  rep(i, 0, h) rep(j, 0, w) cin >> a[i][j];
  int ans = 0;
  rep(starti, 0, h - 2) rep(startj, 0, w - 2) {
    rep(leni, 3, h + 1 - starti) rep(lenj, 3, w + 1 - startj) {
      int _min = 100;
      rep(i, 0, leni) _min = min(_min, a[starti + i][startj]);
      rep(i, 0, leni) _min = min(_min, a[starti + i][startj + lenj - 1]);
      rep(j, 0, lenj) _min = min(_min, a[starti][startj + j]);
      rep(j, 0, lenj) _min = min(_min, a[starti + leni - 1][startj + j]);
      int _max = -1;
      int res = 0;
      rep(i, 0, leni - 2) rep(j, 0, lenj - 2) {
        _max = max(_max, a[starti + 1 + i][startj + 1 + j]);
        res += _min - a[starti + 1 + i][startj + 1 + j];
      }
      if (_min > _max) ans = max(ans, res);
    }
  }
  cout << ans << endl;
}

int main() {
  int h, w;
  while (cin >> h >> w, h) {
    solve(h, w);
  }
}

C++: #include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[]={0, 0, 1, -1, 1, 1, -1, -1};
int dx[]={1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define mp make_pair
#define fi first
#define sc second
#define INF 1000000000
ll h,w;
ll e[50][50];
int main(){
	while(true) {
		cin >> h >> w;

		if(h == 0)
			break;

		REP(i,h) {
			REP(j,w) {
				cin >> e[i][j];
			}
		}

		ll ans = 0;
		REP(i,h) {
			FOR(j,i + 2,h) {
				REP(k,w) {
					FOR(l,k + 2,w) {
						ll miniw = INF;

						FOR(p,i,j + 1) {
							miniw = min(e[p][k],miniw);
							miniw = min(e[p][l],miniw);
						}

						FOR(p,k,l + 1) {
							miniw = min(e[i][p],miniw);
							miniw = min(e[j][p],miniw);
						}

						bool f = false;
						ll cou = 0;
						FOR(p,i + 1,j) {
							FOR(q,k + 1,l) {
								if(miniw <= e[p][q])
									f = true;
								cou += miniw - e[p][q];
							}
						}
						if(f) 
							continue;
						ans = max(cou,ans);

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

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 INF (1<<28)
#define DEKAI 1000000007
#define FDST int hogehogehoge;cin>>hogehogehoge;

int 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;
    for(int z=3;z<=d;z++){
      for(int zz=3;zz<=w;zz++){
	for(int i=0;i<=d-z;i++){
	  for(int j=0;j<=w-zz;j++){
	    int gmin=INF,nmax=-INF;
	    for(int x=i;x<z+i;x++){
	      for(int y=j;y<zz+j;y++){
		if(x==i||y==j||x==z+i-1||y==zz+j-1){
		  gmin=min(gmin,a[x][y]);
		}
		else{
		  nmax=max(nmax,a[x][y]);
		}
	      }
	    }
	    if(gmin<=nmax) continue;
	    int sums=0;
	    for(int x=i;x<z+i;x++){
	      for(int y=j;y<zz+j;y++){
		if(x==i||y==j||x==z+i-1||y==zz+j-1){
		  continue;
		}
		else{
		  sums+=gmin-a[x][y];
		}
	      }
	    }
	    ans=max(sums,ans);
	  }
	}
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}