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,n) for(int i=a; i<n; i++)
#define repq(i,a,n) for(int i=a; i<=n; i++)
typedef long long int ll;
typedef pair<int, int> pii;
int M[10][10];

int calc(int i, int j, int k, int l, int h){
  int ret = 0;
  for(int a = i+1; a < k; ++a){
    for(int b = j+1; b < l; ++b){
      if(M[a][b] >= h) return 0;
      ret += h - M[a][b];
    }
  }
  return ret;
}

int main() {
  int d, w;
  while(cin >> d >> w, d){
    int ans = 0;
    for(int i = 0; i < d; ++i){
      for(int j = 0; j < w; ++j){
	cin >> M[i][j];
      }
    }
    for(int i = 0; i < d-2; ++i){
      for(int j = 0; j < w-2; ++j){
	for(int k = i+2; k < d; ++k){
	  for(int l = j+2; l < w; ++l){
	    int h = 10;
	    for(int m = i; m <= k; ++m){
	      h = min(h,M[m][j]);
	      h = min(h,M[m][l]);
	    }
	    for(int m = j; m <= l; ++m){
	      h = min(h,M[i][m]);
	      h = min(h,M[k][m]);	      
	    }
	    //printf("%d, %d, %d, %d, %d\n",i,j,k,l,h);
	    ans = max(ans, calc(i,j,k,l,h));
	  }
	}
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(long long i = (a); i < (b); i++)
#define rep(i,b) for(long long i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;
 
template <typename X, typename T>
auto vectors(X x, T a) {
	return vector<T>(x, a);
}
template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
	auto cont = vectors(y, z, zs...);
	return vector<decltype(cont)>(x, cont);
}
template<typename T>
ostream& operator << (ostream& os, vector<T>& v){
	rep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? "" : " "); } return os;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& v){
	for(T& x: v){ is >> x; } return is;
}

int m[10][10];

int outer(int ly, int lx, int ry, int rx){
	int mini = 1e9;
	range(i,ly,ry + 1){
		mini = min(mini, m[i][rx]);
		mini = min(mini, m[i][lx]);
	}
	range(i,lx,rx + 1){
		mini = min(mini, m[ry][i]);
		mini = min(mini, m[ly][i]);
	}
	return mini;
}

int inner(int ly, int lx, int ry, int rx, int out){
	int res = 0;
	range(i,ly, ry){
		range(j, lx, rx){
			if(m[i][j] >= out) return -1;
			res += out - m[i][j];
		}
	}
	return res;
}

int main(){
	int h, w;
	while(cin >> h >> w, h){
		int ans = 0;
		rep(i,h){
			rep(j,w){
				cin >> m[i][j];
			}
		}
		rep(ly,h){
			rep(lx,w){
				range(ry,ly + 2, h){
					range(rx,lx + 2, w){
						int out = outer(ly, lx, ry, rx);
						ans = max<int>(ans, inner(ly + 1, lx + 1, ry, rx, out));
					}
				}
			}
		}
		cout << ans << endl;
	}
}

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

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9+7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n"  )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define all(obj) (obj).begin(),(obj).end()
#define rall(obj) (obj).rbegin(),(obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32,int32> pii;
typedef pair<int64,int64> pll;

template<class T> inline bool chmax(T& a, T b) {
  if (a < b) { a = b; return true; } return false;
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) { a = b; return true; } return false;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
        int d,w;cin>>d>>w;
        if(d==0&&w==0){
            break;
        }
        int ans=0;
        vector<vector<int> >c(d,vector<int>(w));
        for(int i=0;i<d;i++){
            for(int j=0;j<w;j++){
                cin>>c[i][j];
            }
        }
        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++){
                        int mi=20;
                        for(int z=k;z<=l;z++){
                            mi=min(mi,c[i][z]);
                            mi=min(mi,c[j][z]);
                        }
                        for(int z=i;z<=j;z++){
                            mi=min(mi,c[z][k]);
                            mi=min(mi,c[z][l]);
                        }
                        int ma=0;
                        int cnt=0;
                        int res=0;
                        for(int x=i+1;x<j;x++){
                            for(int y=k+1;y<l;y++){
                                cnt++;
                                ma=max(c[x][y],ma);
                                res+=c[x][y];
                            }
                        }
                        if(ma<mi){
                            ans=max(ans,cnt*mi-res);
                        }
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
}