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 enld '\n'
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr double INFD = 1e100;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int d,w;
    cin >> d >> w;
    while(d != 0){
        vector<vector<int>> G(d,vector<int> (w));
        for(int i=0; i<d; i++){
            for(int j=0; j<w; j++){
                cin >> G[i][j];
            }
        }
        int 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++){
                        int mn_r = inf;
                        int mx_i = 0;
                        int sum = 0;
                        for(int H=i; H<=j; H++){
                            for(int W=k; W<=l; W++){
                                if(H==i or H==j or W==k or W==l){
                                    chmin(mn_r,G[H][W]);
                                }else{
                                    chmax(mx_i,G[H][W]);
                                    sum += G[H][W];
                                }
                            }
                        }
                        if(mn_r > mx_i){
                            chmax(ans,(j-i-1)*(l-k-1)*mn_r - sum);
                        }
                    }
                }
            }
        }
        cout << ans << enld;
        cin >> d >> w;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <tuple>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

class Solver {
};
int main() {
	while (true) {
		int d,w;
		cin >> d >> w;
		if (d == 0)break;

		vector<vector<int>> grid(d, vector<int>(w, 0));
		for (int i =0 ; i < d; i++) {
			for (int j = 0; j < w; j++) {
				int val;
				cin >> val;
				grid[i][j] = val;
			}
		}

		int val = 0;
		for (int i = 0; i < d; i++) {
			for (int j = 0; j < w; j++) {
				for (int k = i + 2; k < d; k++) {
					for (int l = j + 2; l < w; l++) {
						int bottom = 1e9;
						for (int y = i; y <= k; y++) {
							bottom = min(bottom, grid[y][j]);
							bottom = min(bottom, grid[y][l]);
						}
						for (int x = j; x <= l; x++) {
							bottom = min(bottom, grid[i][x]);
							bottom = min(bottom, grid[k][x]);
						}

						int sum = 0;
						bool valid = true;
						for (int y = i + 1; y < k; y++) {
							for (int x = j + 1; x < l; x++) {
								if (grid[y][x] >= bottom) valid = false;
								else sum += bottom - grid[y][x];
							}
						}
						if (valid)val = max(val, sum);
					}
				}
			}
		}

		cout << val << endl;
	}
}

C++: #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int d,w,map[11][11];
int main(){
    while(cin>>d>>w){
        if(!d&&!w) break;
        memset(map,0,sizeof map);
        for(int i=1;i<=d;i++)
            for(int j=1;j<=w;j++) cin>>map[i][j];
        int ans=0;
        for(int i=1;i<=d-2;i++)
            for(int j=1;j<=w-2;j++)
                for(int x=3;x+i-1<=d;x++)
                    for(int y=3;y+j-1<=w;y++){
                        int minn=100,maxn=-1;
                        for(int z=0;z<x;z++){
                            if(minn>map[i+z][j]) minn=map[i+z][j];
                            if(minn>map[i+z][j+y-1]) minn=map[i+z][j+y-1];
                        }
                        for(int z=0;z<y;z++){
                            if(minn>map[i][j+z]) minn=map[i][j+z];
                            if(minn>map[i+x-1][j+z]) minn=map[i+x-1][j+z];
                        }
                        for(int u=i+1;u<=i+x-2;u++)
                            for(int v=j+1;v<=j+y-2;v++) maxn=max(maxn,map[u][v]);
                        if(maxn>=minn) continue;
                        int ans0=0;
                        if(maxn<=minn){
                            for(int u=i+1;u<=i+x-2;u++)
                                for(int v=j+1;v<=j+y-2;v++) ans0+=minn-map[u][v];
                        }
                        ans=max(ans,ans0);
                    }
        cout<<ans<<endl;
    }
    return 0;
}