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 <cstring>
#include <algorithm>

using namespace std;

const int W = 10, D = 10, E = 10;
int imos[E][D + 1][W + 1];

int main(){
    int d, w, x, ans, imax, omin;
    int outer[E], inner[E];
    while(cin >> d >> w, d){
        memset(outer, 0, sizeof(outer));
        memset(inner, 0, sizeof(inner));
        memset(imos, 0, sizeof(imos));
        ans = 0;
        for(int i = 1;i <= d;++i){
            for(int j = 1;j <= w;++j){
                cin >> x;
                imos[x][i][j] = 1;
            }
        }
        for(int i = 1;i <= d;++i){
            for(int j = 1;j <= w;++j){
                for(x = 0;x < E;++x){
                    imos[x][i][j] += imos[x][i][j - 1];
                }
            }
        }
        for(int i = 1;i <= d;++i){
            for(int j = 1;j <= w;++j){
                for(x = 0;x < E;++x){
                    imos[x][i][j] += imos[x][i - 1][j];
                }
            }
        }
        for(int i = 1;i <= d;++i){
            for(int j = 1;j <= w;++j){
                for(int ii = i + 2;ii <= d;++ii){
                    for(int jj = j + 2;jj <= w;++jj){
                        // cout << i << "," << j << " " << ii << "," << jj << endl;
                        for(x = 0;x < E;++x){
                            outer[x] = imos[x][i - 1][j - 1] - imos[x][i - 1][jj] - imos[x][ii][j - 1] + imos[x][ii][jj];
                            inner[x] = imos[x][i][j] - imos[x][i][jj - 1] - imos[x][ii - 1][j] + imos[x][ii - 1][jj - 1];
                            outer[x] -= inner[x];
                            // cout << outer[x] << "," << inner[x] << endl;
                        }
                        imax = E;
                        for(x = 0;x < E;++x){
                            if(inner[x] != 0) {
                                imax = x;
                            }
                        }
                        for(x = 0;x < E;++x){
                            if(outer[x] != 0) break;
                        }
                        omin = x;
                        // cout << imax << " " << omin << endl;
                        if(imax < omin){
                            // cout << imax << "," << omin << endl;
                            int a = 0;
                            for(x = 0;x < omin;++x){
                                a += (omin - x) * inner[x];
                            }
                            ans = max(ans, a);
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

#define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << endl
#define LINE cout << "line : " << __LINE__ << endl
#define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl;
#define STOP assert(false);
#else
#define dump(x) ;
#define LINE 	;
#define dumpV(v);
#define STOP 	;
#endif

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fi<<", "<<a.se<<')';
    return out;
  }
}


int main(){
	int H, W;
	while(cin >> H >> W) {
		if(H == 0 && W == 0) break;
		vector<vi> d(H);
		rep(i, H) {
			d[i].resize(W);
			rep(j, W) cin >> d[i][j];
		}
		
		auto calc = [&](int x1, int x2, int y1, int y2) {
			int m = 1e9;
			REP(i, x1, x2) m = min({m, d[y1][i], d[y2-1][i]});
			REP(i, y1, y2) m = min({m, d[i][x1], d[i][x2-1]});
			int ret = 0;
			REP(i, y1+1, y2-1) REP(j, x1+1, x2-1) {
				if(d[i][j] >= m) return 0;
				ret += m - d[i][j];
			}
			return ret;
		};

		int ans = 0;
		rep(i, W) REP(j, i+1, W+1) rep(k, H) REP(l, k+1, H+1) {
			ans = max(ans, calc(i, j, k, l));
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  while (true) {
    int h, w; cin >> h >> w;
    if (!h) break;
    VV<> a(h, V<>(w));
    for (auto&& v : a) for (auto&& e : v) cin >> e;
    int res = 0;
    for (int i0 = 0; i0 < h; ++i0) for (int i1 = 0; i1 < h; ++i1) if (i0 < i1) {
      for (int j0 = 0; j0 < w; ++j0) for (int j1 = 0; j1 < w; ++j1) if (j0 < j1) {
        int mn = 1e9;
        for (int j = j0; j <= j1; ++j) mn = min({mn, a[i0][j], a[i1][j]});
        for (int i = i0; i <= i1; ++i) mn = min({mn, a[i][j0], a[i][j1]});
        int mx = -1e9, s = 0;
        for (int i = i0 + 1; i < i1; ++i) for (int j = j0 + 1; j < j1; ++j) {
          mx = max(mx, a[i][j]);
          s += mn - a[i][j];
        }
        if (mn > mx) res = max(res, s);
      }
    }
    cout << res << '\n';
  }
}