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++: #define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;

int main() {
	int h, w;
	while (cin >> h >> w, h) {
		int f[22][22];
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				cin >> f[i][j];
			}
		}
		int ans = 0;
		for (int i = 1; i <= h; ++i) {
			for (int j = 1; j <= w; ++j) {
				for (int ii = 0; ii < i; ++ii) {
					if (i - ii < 3) continue;
					for (int jj = 0; jj < j; ++jj) {
						if (j - jj < 3)continue;
						int soto = 123456;
						int uchi = -1;
						int yous = 0;
						for (int x = ii; x < i; ++x) {
							for (int y = jj; y < j; ++y) {
								if (x == ii || x == i - 1 || y == jj || y == j - 1) {
									soto = min(soto, f[x][y]);
								}
								else {
									uchi = max(uchi, f[x][y]);
									yous -= f[x][y];
								}
							}
						}
						if (soto <= uchi) {
							yous = 0;
						}
						else {
							yous += soto * (i - ii - 2) * (j - jj - 2);
						}
						ans = max(ans, yous);
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e9+100;
const ll MOD = 1e9+7;
const bool debug = false;

int H, W;
int e[10][10];

int main() {
	while (cin >> H >> W, H || W) {
		REP(i, H) REP(j, W) scanf("%d", &e[i][j]);
		if (debug) putchar('@');

		int ans = 0;
		FOR(h, 3, H + 1) FOR(w, 3, W + 1) { // 1-index
			REP(y, H - h + 1) REP(x, W - w + 1) {
				int mi = INF, ma = 0;
				int sum = 0;
				REP(i, h) REP(j, w) {
					int ny = y + i, nx = x + j;
					if (i == 0 || j == 0 || i == h - 1 || j == w - 1) {
						chmin(mi, e[ny][nx]);
					}
					else {
						chmax(ma, e[ny][nx]);
						sum -= e[ny][nx];
					}
				}
				sum += (h - 2) * (w - 2) * mi;
				if (mi <= ma) continue;
				chmax(ans, sum);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cmath>
#include<cassert>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)

using namespace std;

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

int main() {
  while( cin >> h >> w, h|w ) {
    rep(i,h) rep(j,w) cin >> e[i][j];
    int maxi = 0;
    rep(y1,h) if( y1+2 < h ) {
      rep(x1,w) if( x1+2 < w ) {
	REP(y2,y1+2,h) {
	  REP(x2,x1+2,w) {
	    int high = -1;
	    int lowe = 12345678;
	    int score = 0;
	    REP(y,y1,y2+1) {
	      REP(x,x1,x2+1) {
		if( y == y1 || y == y2 || x == x1 || x == x2 ) lowe = min(lowe,e[y][x]);
		else high = max(high,e[y][x]);
	      }
	    }
	    if( high >= lowe ) continue;
	    REP(y,y1,y2+1) {
	      REP(x,x1,x2+1) {
		if( y == y1 || y == y2 || x == x1 || x == x2 ) ;
		else { score += ( lowe - e[y][x] ); }
	      }
	    }	    
	    maxi = max(maxi,score);
	  }
	}
      }
    }




    cout << maxi << endl;
  }
  return 0;
}