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 int long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(),a.end()
const long long MOD = 1e9+7, INF = 1e18;
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 H,W;

int check(int si,int h,int sj,int w,vector<vector<int>>&v){
	int ma = -INF,mi = INF;
	rep(i,si,h+1){
		rep(j,sj,w+1){
			if(i==si||j==sj||i==h||j==w)chmin(mi,v[i][j]);
			else chmax(ma,v[i][j]);
		}
	}
	if(ma>=mi)return 0;
	int ret = 0;
	rep(i,si+1,h){
		rep(j,sj+1,w){
			ret += mi - v[i][j];
		}
	}
	return ret;
}

signed main(){
	vector<int>ans;
	while(cin>>H>>W,H||W){
		vector<vector<int>>v(H,vector<int>(W,0));
		rep(i,0,H){
			rep(j,0,W){
				cin>>v[i][j];
			}
		}
		int ma = 0;
		rep(i,0,H){
			rep(j,i+2,H){
				rep(k,0,W){
					rep(l,k+2,W){
						chmax(ma,check(i,j,k,l,v));
					}
				}
			}
		}
		ans.push_back(ma);
	}
	for(auto e:ans){
		cout<<e<<endl;
	}
}

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}

int main(void) {
	int64 h, w;
	while (cin >> h >> w && h+w) {
		auto f = make_v<int64>(h, w);
		REP(i, h) REP(j, w) cin >> f[i][j];
		int64 res = 0;
		REP(lu_y, h) {
			REP(lu_x, w) {
				FOR(rd_y, lu_y+2, h) {
					FOR(rd_x, lu_x+2, w) {
						int64 mini = INF, maxi = 0, sum = 0;
						FOR(y, lu_y, rd_y+1) {
							FOR(x, lu_x, rd_x+1) {
								if (y == lu_y || y == rd_y || x == lu_x || x == rd_x)
									mini = min(mini, f[y][x]);
								else {
									maxi = max(maxi, f[y][x]);
									sum += f[y][x];
								}
							}
						}
						if (maxi < mini)
							res = max(res, (rd_x-lu_x-1)*(rd_y-lu_y-1)*mini-sum);
					}
				}
			}
		}
		cout << res << endl;
	}
}

C++: #include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
int d,w;
int maze[15][15];
int main(){
    while(1){
        int d,w;
        cin>>d>>w;
        if(d==0 && w==0) break;
        for(int i=0;i<d;i++){
            for(int j=0;j<w;j++) cin>>maze[i][j];
        }
        int max=0;
        for(int i=1;i<d-1;i++){
            for(int j=1;j<w-1;j++){
                for(int k=i;k<d-1;k++){
                    for(int l=j;l<w-1;l++){
                        int min=inf;
                        for(int m=i-1;m<=k+1;m++){
                            if(maze[m][j-1]<min) min=maze[m][j-1];
                            if(maze[m][l+1]<min) min=maze[m][l+1];
                        }
                        for(int n=j-1;n<=l+1;n++){
                            if(maze[i-1][n]<min) min=maze[i-1][n];
                            if(maze[k+1][n]<min) min=maze[k+1][n];
                        }
                        //周囲のminが求まる.
                        bool ok=true;
                        int cnt=0;
                        for(int m=j;m<=l;m++){
                            for(int n=i;n<=k;n++){
                                if(maze[n][m]>=min) ok=false;
                                else cnt+=min-maze[n][m];
                            }
                        }
                        //cout<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<ok<<endl;
                        //cout<<cnt<<" "<<min<<endl;
                        if(ok && cnt>=max) {
                            max=cnt;
                            //cout<<i<<" "<<j<<" "<<k<<" "<<l<<endl;
                        }
                    }
                }
            }
        }
        cout<<max<<endl;
    }
    return 0;
}