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 solve(int d, int w) {
    int e[d][w];
    for (int i = 0; i < d; i++) {
        for (int j = 0; j < w; j++) {
            cin >> e[i][j];
        }
    }
    int ans = 0;
    for (int x1 = 0; x1 < d; x1++) {
        for (int x2 = 0; x2 < d; x2++) {
            if (x2 - x1 < 2) continue;
            for (int y1 = 0; y1 < w; y1++) {
                for (int y2 = 0; y2 < w; y2++) {
                    if (y2 - y1 < 2) continue;
                    int inner_height = 0;
                    int surround_height = 100;
                    int inner_sum = 0;
                    int counter = 0;
                    for (int i = 0; i < d; i++) {
                        for (int j = 0; j < w; j++) {
                            if (x1 < i && i < x2 && y1 < j && j < y2) {
                                inner_height = max(inner_height, e[i][j]);
                                inner_sum += e[i][j];
                                counter++;
                            }
                            if (x1 <= i && i <= x2 && (j == y1 || j == y2)) {
                                surround_height = min(surround_height, e[i][j]);
                            }
                            if (y1 <= j && j <= y2 && (i == x1 || i == x2)) {
                                surround_height = min(surround_height, e[i][j]);
                            }
                        }
                    }
                    if (surround_height > inner_height) {
                        int tmp = counter * surround_height - inner_sum;
                        ans = max(ans, tmp);
                    }
                }
            }
        }
    }
    return ans;
}

int main() {
    int d, w;
    while (true) {
        cin >> d >> w;
        if (!d && !w) return 0;
        cout << solve(d, w) << endl;
    }
}

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

#define rep(i,n) for(int i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(int i=(a),i##_cond=(b);i<i##_cond;++i)
using vi  = vector<int>;
using vvi = vector<vi>;
constexpr int inf = 1<<30;
template<class T>bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T>bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}

vvi f;

int calc_min(int x, int y, int h, int w){
  int minv = inf;
  FOR(i,x,x+h+2){
    chmin(minv, f[i][y]);
    chmin(minv, f[i][y+w+1]);
  }
  FOR(j,y,y+w+2){
    chmin(minv, f[x][j]);
    chmin(minv, f[x+h+1][j]);
  }
  return minv;
}
int calc_max(int x, int y, int h, int w){
  int maxv = -inf;
  FOR(i,x+1,x+h+1) FOR(j,y+1,y+w+1)
    chmax(maxv, f[i][j]);
  return maxv;
}
int calc_cap(int x, int y, int h, int w, int minv){
  int ans = 0;
  FOR(i,x+1,x+h+1) FOR(j,y+1,y+w+1)
    ans += minv - f[i][j];
  return ans;
}

int main(){
  int a, b;
  while(cin >> a >> b, a){
    f.clear();
    f.resize(a, vi(b,0));
    rep(i,a) rep(j,b) cin >> f[i][j];

    int ans = 0;
    FOR(x,0,a-1) FOR(y,0,b-1){ // 枠の左上の点
      FOR(h,1,a-x-1) FOR(w,1,b-y-1){ // 水の幅
	int minv = calc_min(x,y,h,w);
	if(minv <= calc_max(x,y,h,w)) continue;
	chmax(ans, calc_cap(x,y,h,w,minv));
      }
    }
    cout << ans << endl;
  }
}