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;

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

typedef long long LL;

int main(){
  int d,w;
  while(1){
    cin >> d >> w;
    if(d==0) return 0;
    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];
      }
    }
    vector<vector<vector<int>>> yokomin(d,vector<vector<int>>(w,vector<int>(w,10)));
    vector<vector<vector<int>>> tatemin(w,vector<vector<int>>(d,vector<int>(d,10)));
    for(int i=0;i<d;i++){
      for(int j=0;j<w;j++){
        for(int k=j+2;k<w;k++){
          for(int l=j;l<=k;l++){
            yokomin[i][j][k]=min(yokomin[i][j][k],e[i][l]);
          }
        }
      }
    }
    for(int i=0;i<w;i++){
      for(int j=0;j<d;j++){
        for(int k=j+2;k<d;k++){
          for(int l=j;l<=k;l++){
            tatemin[i][j][k]=min(tatemin[i][j][k],e[l][i]);
          }
        }
      }
    }
    vector<vector<vector<int>>> yokomax(d,vector<vector<int>>(w,vector<int>(w,0)));
    for(int i=0;i<d;i++){
      for(int j=0;j<w;j++){
        for(int k=j;k<w;k++){
          for(int l=j;l<=k;l++){
            yokomax[i][j][k]=max(yokomax[i][j][k],e[i][l]);
          }
        }
      }
    }
    //  添え磁気圏
    vector<vector<int>> ruiseki(d+1,vector<int>(w+1,0));
    for(int i=1;i<=d;i++){
      for(int j=1;j<=w;j++){
        ruiseki[i][j]=ruiseki[i-1][j]+ruiseki[i][j-1]-ruiseki[i-1][j-1]+e[i-1][j-1];
      }
    }
    int ans=0;
    int mi=10,ma;
    for(int i=0;i<d;i++){
      for(int j=0;j<w;j++){
        for(int k=i+2;k<d;k++){
          for(int l=j+2;l<w;l++){
            mi=yokomin[i][j][l];
            mi=min(mi,yokomin[k][j][l]);
            mi=min(mi,tatemin[j][i][k]);
            mi=min(mi,tatemin[l][i][k]);
            ma=0;
            for(int x=i+1;x<=k-1;x++){
              ma=max(ma,yokomax[x][j+1][l-1]);
            }
            if(mi>ma){
              ans=max(ans,mi*(k-i-1)*(l-j-1)-(ruiseki[k][l]-ruiseki[i+1][l]-ruiseki[k][j+1]+ruiseki[i+1][j+1]));
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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<iostream>
using namespace std;
#define rep(i, a, n) for(int i = (a); i < (n); i++)

int main(){
    int d, w;
    while(cin >> d >> w, d+w){
        int mat[d][w];
        for(int i = 0; i < d*w; i++)    cin >> mat[i/w][i%w];

        int ans = 0;

        rep(i, 0, d) rep(j, 0, w){
            rep(ni, i+2, d) rep(nj, j+2, w){
                int surround = 10, water = 0, lakesum = 0;
                rep(di, i, ni+1) rep(dj, j, nj+1){
                    if(di == i || di == ni || dj == j || dj == nj){
                        surround = min(surround, mat[di][dj]);
                    }else{
                        water = max(water, mat[di][dj]); 
                        lakesum += mat[di][dj];
                    }
                }

                if(surround <= water)   continue;

                int lakeA = (ni-i-1)*(nj-j-1);
                ans = max(ans, surround*lakeA - lakesum);
            }
        }

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