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"

#define REP(i,n) for(ll i=0;i<ll(n);++i)
#define RREP(i,n) for(ll i=ll(n)-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<ll(n);++i)
#define RFOR(i,m,n) for(ll i=ll(n)-1;i>=ll(m);--i)
#define ALL(v) (v).begin(),(v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
const int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;



int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int d, w;
	while (cin >> d >> w, d) {
		vvi v(d, vi(w));
		int ans = 0;
		REP(i, d)REP(j, w)cin >> v[i][j];
		REP(i, d - 2)REP(j, w - 2)FOR(x, i + 2, d)FOR(y, j + 2, w) {
			int m = INF;
			for (int k = i; k <= x; ++k) {
				m = min(m, v[k][j]);
				m = min(m, v[k][y]);
			}

			for (int k = j; k <= y; ++k) {
				m = min(m, v[i][k]);
				m = min(m, v[x][k]);
			}
			bool ok = true;
			int sum = 0;
			FOR(l, i + 1, x)FOR(k, j + 1, y) {
				if (v[l][k] >= m)ok = false;
				else sum += m - v[l][k];
			}
			if (ok)ans = max(ans, sum);
		}
		cout << ans << endl;
	}

}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int table[20][20];
int d,w;
const int N=-1;

int check(int y,int x,int dy,int dx){
	int cntmax=N;
	for(int i=y;i<=y+dy;i++){
		for(int j=x;j<=x+dx;j++)cntmax=max(cntmax,table[i][j]);
	}
	int cntmin=1000000;
	for(int i=y-1;i<=y+dy+1;i++){cntmin=min(cntmin,table[i][x-1]);cntmin=min(cntmin,table[i][x+dx+1]);}
	for(int i=x-1;i<=x+dx+1;i++){cntmin=min(cntmin,table[y-1][i]);cntmin=min(cntmin,table[y+dy+1][i]);}
	if(cntmax>=cntmin)return N;
	
	int sum=0;
	for(int i=y;i<=y+dy;i++){
		for(int j=x;j<=x+dx;j++)sum+=cntmin-table[i][j];
	}
	return sum;
}

int main(){
	while(cin>>d>>w,d||w){
	for(int i=0;i<=d+1;i++)for(int j=0;j<=w+1;j++)table[i][j]=N;
	for(int i=1;i<=d;i++){
		for(int j=1;j<=w;j++)cin>>table[i][j];
	}
	int ans=0;
	for(int i=2;i<d;i++){
		for(int j=2;j<w;j++){
			for(int y=0;y+i<d;y++){
				for(int x=0;x+j<w;x++){
						
						ans=max(ans,check(i,j,y,x));//i,j??????i+y,x+j?????????????±???¨??????????????????????????¢?????????????????????????????°-1?????????
					}
			}
		}
	}
	cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int d,w;
int cell[100];

int solve(int x,int y,int width){
  //widthは塀を含む
  int max_vol=0;
  for(int height=3;y+height<=d;++height){
    int min_height=10;
    for(int i=0;i<width;++i)
      min_height=min(min(cell[x+i+(y+height-1)*w],cell[x+i+y*w]),min_height);
    for(int i=0;i<height;++i)
      min_height=min(min(cell[x+(y+i)*w],cell[x+width-1+(y+i)*w]),min_height);

    bool canCreate=true;
    int vol=0;
    for(int i=1;i<=width-2;++i)
      for(int j=1;j<=height-2;++j){
        if(min_height<=cell[x+i+(y+j)*w])
          canCreate=false;
        else
          vol+=min_height-cell[x+i+(y+j)*w];
      }
    if(canCreate){
      max_vol=max(max_vol,vol);
    }
  }
  return max_vol;
}
int main(){
  while(true){
    cin>>d>>w;
    if(d==0&&w==0)
      break;
    for(int y=0;y<d;++y)
      for(int x=0;x<w;++x){
        int e;
        cin>>e;
        cell[x+y*w]=e;
      }
    int max_vol=0;
    for(int y=0;y<d;++y)
      for(int x=0;x<w;++x)
        for(int width=3;x+width<=w;++width)
          max_vol=max(max_vol,solve(x,y,width));
    cout<<max_vol<<endl;
  }
  return 0;
}

C++: #include<cstdio>
#include <iostream>
#include<cmath>
#include<set>
#include<cstdio>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<string>
#include<cstring>

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

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1.0);

int a[11][11], w, h;

int chp(int y1, int y2, int x1, int x2) {
	int res = 0, ans = 0, t = 1e9;
	for (int i = y1; i <= y2; i++) {//寻找此区域内的最大高度
		for (int j = x1; j <= x2; j++) {
			res = max(res, a[i][j]);
		}
	}

	//检查是否能够围成池塘，并寻找池塘中水平面的最高高度
	for (int i = y1 - 1; i <= y2 + 1; i++) {
		for (int j = x1 - 1; j <= x2 + 1; j++) {
			if (i == y1 - 1 || i == y2 + 1 || j == x1 - 1 || j == x2 + 1) {
				if (a[i][j] <= res)return 0;
				t = min(t, a[i][j]);
			}
		}
	}

	//计算池塘容积
	for (int i = y1; i <= y2; i++) {
		for (int j = x1; j <= x2; j++) {
			ans += t - a[i][j];
		}
	}
	return ans;
}

int ch(int y, int x) {
	int ans = 0;

	//枚举池塘
	for (int i = y; i <= h - 2; i++) {
		for (int j = x; j <= w - 2; j++) {
			ans = max(ans, chp(y, i, x, j));
		}
	}
	return ans;
}

int main() {
	while (cin >> h >> w, h) {
		int ans = 0;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> a[i][j];
			}
		}
		for (int i = 0; i < h - 1; i++) {
			for (int j = 0; j < w - 1; j++) {
				if (i&&j)ans = max(ans, ch(i, j));
			}
		}
		cout << ans << endl;
	}
	return 0;
}