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>
const int INF = 1e9;
const int MOD = 1e9+7;
const long long LINF = 1e18;
#define dump(x)  cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define FOREACH(x,a) for(auto& (x) : (a) )
typedef long long ll;
using namespace std;

// [l, r]の累積min
int h_accum_min(vector<vector<int> > &c, int y, int l, int r, int v, int h) {
    int ret = INF;
    for (int i = l; i <= r; ++i) {
        ret = min(ret, c[y][i]);
    }
    return ret;
}

// [u, d]の累積min
int v_accum_min(vector<vector<int> > &c, int x, int u, int d, int v, int h) {
    int ret = INF;
    for (int i = u; i <= d; ++i) {
        ret = min(ret, c[i][x]);
    }
    return ret;
}

int main(int argc, char const *argv[]) {
    while (true) {
        int d, w;
        cin >> d >> w;
        if (d == 0 && w == 0) break;
        vector<vector<int> > c(d, vector<int>(w));
        vector<vector<int> > v(d, vector<int>(w, 0));
        vector<vector<int> > h(d, vector<int>(w, 0));
        REP(i,d) {
            REP(j,w) {
                cin >> c[i][j];
            }
        }
        int ans = 0;
        // 縦横に動く左上
        for (int i = 0; i < d-2; ++i) {
            for (int j = 0; j < w-2; ++j) {
                // 右上は右に動くだけ。下降は左上に追随する
                for (int ru = j+2; ru < w; ++ru) {
                    // 左下は下に動くだけ。右に動くのは左上に追随する
                    for (int ld = i+2; ld < d; ++ld) {
                        int tmp = 0;
                        bool ok = true;
                        int U = h_accum_min(c, i, j, ru, d, w);
                        int D = h_accum_min(c, ld, j, ru, d, w);
                        int L = v_accum_min(c, j, i, ld, d, w);
                        int R = v_accum_min(c, ru, i, ld, d, w);
                        int MIN = min(U, min(D, min(L, R)));
                        // printf("i = %d, j = %d, ru = %d, ld = %d\n", i, j, ru, ld);
                        // cout << "MIN = " << MIN << endl;
                        // 内部を探索する。右下は左下と右上に追随するので記述しなくて良い
                        for (int in = i+1; in < ld; ++in) {
                            if (!ok) break;
                            for (int jn = j+1; jn < ru; ++jn) {
                                // printf("in = %d, jn = %d, c[in][jn] = %d\n", in, jn, c[in][jn]);
                                if (c[in][jn] >= MIN) {
                                    ok = false;
                                    break;
                                }
                                tmp += (MIN-c[in][jn]);
                                // cout << "tmp = " << tmp << endl;
                            }
                        }

                        if (ok) {
                            ans = max(ans, tmp);
                        }
                    }
                }
            }
        }

        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<deque>
#include<regex>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<iomanip>

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

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);

int main() {
    int d,w;
    while(true){
        cin>>d>>w;
        if(d==0||w==0)break;
        int ike[d][w];
        rep(i,d){ rep(j,w){ cin>>ike[i][j]; }}
        int ans=0;
        for(int iw=3;iw<=w;iw++){
            for(int id=3;id<=d;id++){
                for(int x=0;x<=w-iw;x++){
                    for(int y=0;y<=d-id;y++){
                        int water=0;
                        int masu=0;
                        int sotom=INF;int nakam=0;

                        for(int i=x;i<x+iw;i++){
                            for(int j=y;j<y+id;j++){
                                if(i==x||i==x+iw-1){
                                    sotom=min(sotom,ike[j][i]);
                                }else if(j==y||j==y+id-1){
                                    sotom=min(sotom,ike[j][i]);
                                }else{
                                    water+=ike[j][i];
                                    masu++;
                                    nakam=max(nakam,ike[j][i]);
                                }
                            }
                        }
                        if(nakam<sotom){
                            int res=masu*sotom-water;
                            ans=max(res,ans);
                        }
                    }
                }
            }
        }

        cout<<ans<<endl;






    }



    return 0;
}