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 INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll d,w;
  while(cin>>d>>w,d+w){
    ll e[d+1][w+1];
    for(int i=0;i<d;i++){
      for(int j=0;j<w;j++){
	cin>>e[i][j];
      }
    }
    ll ans=0;
    for(int lx=0;lx<d-2;lx++){
      for(int ly=0;ly<w-2;ly++){
	for(int rx=lx+2;rx<d;rx++){
	  for(int ry=ly+2;ry<w;ry++){
	    ll mi=llINF;
	    for(int i=lx;i<=rx;i++){
	      mi=min({mi,e[i][ly],e[i][ry]});
	    }
	    for(int i=ly;i<=ry;i++){
	      mi=min({mi,e[lx][i],e[rx][i]});
	    }
	    bool fl=true;
	    ll sum=0;
	    for(int i=lx+1;i<rx;i++){
	      for(int j=ly+1;j<ry;j++){
		if(mi<=e[i][j])fl=false;
		sum+=mi-e[i][j];
	      }
	    }
	    if(fl){ans=max(ans,sum);}
	  }
	}
      }
    }
    cout<<ans<<endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int D, W, e;
int A[11][11];
int a[11][11];
int S[11][11];
int s[11][11];

int menseki (int lx, int ly, int rx, int ry) {
	int R = s[ry][rx] - s[ly - 1][rx] - s[ry][lx - 1] + s[ly - 1][lx - 1];
	return R;
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    


int solve() {
	int ans = 0;
	for (int d = 0; d <= D; d++) {
		for (int w = 0; w <= W; w++) {
			if (d * w == 0) {
				S[d][w] = 0;
				continue;
			}
			cin >> e;
			A[d][w] = e;
			S[d][w] = e + S[d - 1][w] + S[d][w - 1] - S[d - 1][w - 1];
			
		}
	}
	
	
	for (int v = 1; v <= 9; v++) {
		for (int d = 0; d <= D; d++) {
			for (int w = 0; w <= W; w++) {
				if (d * w == 0) {
					s[d][w] = 0;
					continue;
				}
				
				if (v > A[d][w]) {
					a[d][w] = 1;
					s[d][w] = 1 + s[d - 1][w] + s[d][w - 1] - s[d - 1][w - 1];
				} else {
					a[d][w] = 0;
					s[d][w] = 0 + s[d - 1][w] + s[d][w - 1] - s[d - 1][w - 1];
				}
			}
		}
		/*cout << "v" << v;
		for (int d = 0; d <= D; d++) {
			for (int w = 0; w <= W; w++) {
				cout << s[d][w] << " ";
			}
			cout << endl;
		}
		*/
		for (int lx = 2; lx < W ; lx++) {
			for (int by = 2; by < D ; by++) {
				for (int rx = lx; rx < W ; rx ++) {
					for (int ty = by; ty < D ; ty++) {
						if (menseki(lx, by, rx, ty) == (rx - lx + 1) * (ty - by + 1) and
							menseki(lx - 1, by - 1, rx + 1, ty + 1) == (rx - lx + 1) * (ty -by + 1)) {
								ans = max(ans, v * (rx - lx + 1) * (ty - by + 1) - (S[ty][rx] - S[by - 1][rx] - S[ty][lx - 1] + S[by - 1][lx - 1]));
						}
					}
				}
			}
		}
	}
	cout << ans << endl;
}


int main() {
	cin >> D >> W;
	while (D > 0 and W > 0) {
		solve();
		cin >> D >> W;
	}
}