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;

int simulate(vector<vector<int>> &garden, int x0, int x1, int y0, int y1) 
{
    // [x0,x1) [y0,y1) に池を作った時の最大容量
    // 内側のマスが外側のマスと同じ大きさがそれより大きい、もしくは外側のマスが内側より小さい時は-1を返す

    int inner_max = -1000000, outer_min = 1000000;
    for(int i = x0; i < x1; ++i) {
        for(int j = y0; j < y1; ++j) {
            if(x0 < i && i < x1-1 && y0 < j && j < y1-1) {
                inner_max = max(inner_max, garden[i][j]);
            } else {
                outer_min = min(outer_min, garden[i][j]);
            }
        }
    }
    /*
    cout << "x0:" << x0 << " x1:" << x1 << " y0:" << y0 << " y1:" << y1 << endl;
    cout << "inner_max:" << inner_max << " outer_min:" << outer_min << endl;
    */
    if(inner_max >= outer_min) return -1;
    int res = 0;
    for(int i = x0+1; i < x1-1; ++i) {
        for(int j = y0+1; j < y1-1; ++j) {
            res += outer_min - garden[i][j];
        }
    }
    return res;
}

bool solve()
{
    int d, w;
    cin >> d >> w;
    if(d == 0 && w == 0) return false;

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

    int res = 0;
    for(int x0 = 0; x0 < d; ++x0) {
        for(int x1 = x0+3; x1 <= d; ++x1) {
            for(int y0 = 0; y0 < w; ++y0) {
                for(int y1 = y0+3; y1 <= w; ++y1) {
                    res = max(res, simulate(garden, x0, x1, y0, y1));
                }
            }
        }
    }

    cout << res << endl;
    return true;
}

int main() {
    while(solve());
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
	int D, W;
	while (cin >> D >> W, D) {
		vector<vector<int> > e(D, vector<int>(W));
		for (int i = 0; i < D; ++i)
			for (int j = 0; j < W; ++j)
				cin >> e[i][j];
		int ans = 0;
		for (int u = 0; u < D; ++u) {
			for (int d = u + 2; d < D; ++d) {
				for (int l = 0; l < W; ++l) {
					for (int r = l + 2; r < W; ++r) {
						int mini = 1e9;
						for (int x = u; x <= d; ++x) mini = min({ mini, e[x][l], e[x][r] });
						for (int y = l; y <= r; ++y) mini = min({ mini, e[u][y], e[d][y] });
						bool flag = true;
						int tmp = 0;
						for (int x = u + 1; x < d; ++x) {
							for (int y = l + 1; y < r; ++y) {
								if (e[x][y] >= mini) {
									tmp = 0, flag = false;
									goto en;
								} else tmp += mini - e[x][y];
							}
						}
						en:
						ans = max(ans, tmp);
					}
				}
			}
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

int solve_testcase() {
    int H, W; scanf("%d%d", &H, &W);
    if(H == 0 and W == 0) return 1;

    vector< vector<int> > board(H, vector<int>(W));
    for(int i=0; i<H; i++) {
        for(int j=0; j<W; j++) {
            scanf("%d", &board[i][j]);
        }
    }

    int ans = 0;
    for(int h=3; h<=H; h++) {
        for(int w=3; w<=W; w++) {
            for(int lx=0; lx<H-h+1; lx++) {
                for(int ly=0; ly<W-w+1; ly++) {
                    int max_inside = -1, min_edge = 100, sum = 0;
                    for(int x=lx; x<lx+h; x++) {
                        for(int y=ly; y<ly+w; y++) {
                            bool edge = false;
                            edge |= (x == lx or x == lx+h-1);
                            edge |= (y == ly or y == ly+w-1);
                            if(edge) chmin(min_edge, board[x][y]);
                            else {
                                chmax(max_inside, board[x][y]);
                                sum += board[x][y];
                            }
                        }
                    }
                    if(max_inside < min_edge) {
                        int cand = min_edge * (h-2) * (w-2) - sum;
                        chmax(ans, cand);
                    }
                }
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

int main() {
    while(!solve_testcase());
    return 0;
}