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 <iostream>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <climits>
//#include "h.h"
using namespace std;

#define ll long long
#define pint pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define pm(first, second) pb(mp(first, second))
#define SPACE " "
#define fpf first.first
#define fps first.second
#define spf second.first
#define sps second.second

#define all(X) (X).begin(), (X).end()

int main(){

	while(true){
		int d, w;
		cin >> d >> w;
		if(d == 0 && w == 0)break;
		vector<vector<int>> ELEV(d, vector<int>(w));
		for(int i = 0; i < d; i ++){
			for(int j = 0; j < w; j ++){
				cin >> ELEV[i][j];
			}
		}
		int ans = 0;
		for(int d1 = 0; d1 < d - 2; d1 ++){
		for(int w1 = 0; w1 < w - 2; w1 ++){
			for(int d2 = d1 + 2; d2 < d; d2 ++){
			for(int w2 = w1 + 2; w2 < w; w2 ++){
				int edgeMin = 100;
				for(int i = w1; i <= w2; i ++){
					edgeMin = min(edgeMin, ELEV[d1][i]);
					edgeMin = min(edgeMin, ELEV[d2][i]);
				}
				for(int i = d1; i <= d2; i ++){
					edgeMin = min(edgeMin, ELEV[i][w1]);
					edgeMin = min(edgeMin, ELEV[i][w2]);
				}
				int tmp = 0;
				bool flag = true;
				for(int i = d1 + 1; i < d2 && flag; i ++){
					for(int j = w1 + 1; j < w2 && flag; j ++){
						if(ELEV[i][j] >= edgeMin){
							flag = false;
							break;
						}
						tmp += edgeMin - ELEV[i][j];
					}
				}
				if(flag)ans = max(ans, tmp);
			}
			}
		}
		}
		cout << ans << endl;
	}

	return 0;
}

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

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout.precision(12);
    cout.setf(ios_base::fixed, ios_base::floatfield);
    
    int d, w;
    int e[10][10];
    while(true){
        cin >> d >> w;
        if(d == 0 && w == 0) break;
        for(int i=0;i<d;i++){
            for(int j=0;j<w;j++) cin >> e[i][j];
        }

        int64_t ans = 0;
        for(int i1=0;i1<d-2;i1++){
            for(int j1=0;j1<w-2;j1++){
                for(int i2=i1+2;i2<d;i2++){
                    for(int j2=j1+2;j2<w;j2++){
                        int min_b = e[i1][j1];
                        int64_t vol = 0;
                        for(int ii=i1;ii<=i2;ii++){
                            min_b = min({min_b, e[ii][j1], e[ii][j2]});
                        }
                        for(int jj=j1;jj<=j2;jj++){
                            min_b = min({min_b, e[i1][jj], e[i2][jj]});
                        }
                        for(int ii=i1+1;ii<i2;ii++){
                            for(int jj=j1+1;jj<j2;jj++){
                                if(min_b - e[ii][jj] <= 0) goto next;
                                vol += min_b - e[ii][jj];
                            }
                        }
                        ans = max(ans, vol);
                        next:;
                    }
                }
            }
        }

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

C++: #include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, SZ(s)) {
    cout << s[i] << " ";
  }
  cout << endl;
}

int main () {
  cin.tie(0);
  cout << setprecision(10);

  while(1) {
    int h, w; cin >> h >> w;
    if(h == 0 && w == 0) break;
    vvi a(h);
    REP(i, h) {
      a[i].resize(w);
      REP(j, w) {
        cin >> a[i][j];
      }
    }
    int ma = 0;
    FOR(i, 3, h+1) {
      FOR(j, 3, w+1) {
        REP(k, h-i+1) {
          REP(l, w-j+1) {
            int mi = INF;
            FOR(m, k, k+i) {
              FOR(n, l, l+j) {
                if(m == k || m == k+i-1 || n == l || n == l+j-1) {
                  mi = min(mi, a[m][n]);
                }
              }
            }
            int sz = 0;
            bool ok = true;
            FOR(m, k+1, k+i-1) {
              FOR(n, l+1, l+j-1) {
                if(a[m][n] >= mi) {
                  ok = false;
                  break;
                } else {
                  sz += mi - a[m][n];
                }
              }
              if(!ok) {
                sz = 0;
                break;
              }
            }
            ma = max(ma, sz);
          }
        }
      }
    }
    cout << ma << endl;
  }
}