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 rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define rrep(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(),greater<int>());
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define ret return
#define lb(v,n) lower_bound(v.begin(),v.end(),n)
#define ub(v,n) upper_bound(v.begin(),v.end(),n)
#define mae(v) max_element(v.begin(),v.end())
#define mie(v) min_element(v.begin(),v.end())
#define INF 0x7FFFFFFF
#define mod 1000000007
#define fir first
#define sec second
typedef long long ll;
////////////////////////////////////////////////////////////

int main(){
	while(1){
		int d,w;
		cin >> d >> w;
		if( d == 0 ) break;
		int e[10][10];
		rep(i,0,d) rep(j,0,w) cin >> e[i][j];

		ll ans = 0;
		for(int s_d = 0 ; s_d < d ; s_d++ ){
			for(int s_w = 0 ; s_w < w ; s_w++ ){
				for(int g_d = s_d+2 ; g_d < d; g_d++ ){
					for(int g_w = s_w+2 ; g_w < w ; g_w++){
						int min_h = INF;

						for(int j = s_w ; j <= g_w ; j++ ){
							min_h = min(e[s_d][j] , min_h );
							min_h = min(e[g_d][j] , min_h );
						}


						for(int i = s_d ; i <= g_d ; i++ ){
							min_h = min(e[i][s_w] , min_h );
							min_h = min(e[i][g_w] , min_h );
						}

						bool f = 0;
						ll sum = 0;
						for(int i = s_d+1; i <= g_d-1 ; i++ ){
							for( int j = s_w+1 ; j <= g_w-1 ; j++ ){
								if( e[i][j] >= min_h ) f = 1;
								sum += min_h - e[i][j];
							}
						}

						//cout << s_d << " " << s_w << " " << g_d << " " << g_w << " " << min_h << " " << sum << endl;
						if( f ) continue;

						ans = max( ans , sum );
					}
				}
			}
		}
		cout << ans << endl;
	}
	ret 0;
}

C++: #define  _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"
#define int ll

using namespace std;
typedef string::const_iterator State;
#define eps 1e-8L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()
void init() {
	iostream::sync_with_stdio(false);
	cout << fixed << setprecision(20);
}
int grids[100][100];
void solve() {
	int d, w;
	while (cin >> d >> w) {
		if (d == 0) return;
		REP(i, d) {
			REP(q, w) {
				cin >> grids[i][q];
			}
		}
		int ans = 0;
		REP(l_h,d) {
			for (int l_r = l_h+2; l_r < d; ++l_r) {
				REP(w_l, w) {
					for (int w_r = w_l+2; w_r < w; ++w_r) {
						int kakomi = 1e9;
						int cnter = 0;
						int naka_max = 0;
						for (int x = l_h; x <= l_r; ++x) {
							for (int y = w_l; y <= w_r; ++y) {
								if (x == l_h or x == l_r or y == w_l or y == w_r) {
									kakomi = min(kakomi, grids[x][y]);
								}
								else {
									naka_max = max(naka_max, grids[x][y]);
									cnter += grids[x][y];
								}
							}
						}
						if (naka_max < kakomi) {
							ans = max(ans, (l_r - l_h - 1) * (w_r - w_l - 1) * kakomi - cnter);
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
}
#undef int
int main() {
	init();
	solve();
}

C++: #include<iostream>
 
int garden[10][10] = {};
int min = 10;
 
int pond_area(int sx,int sy,int width,int height){
  int area = 0;
  for(int i=sx+1;i<width+sx-1;i++){
    for(int j=sy+1;j<height+sy-1;j++){
      int d = garden[j][i];
      area += (min - d);
    }
  }
  return area;
}
     
bool isPond(int sx,int sy,int width,int height){
  //std::cout << "start:(" << sx << ',' << sy << ") width = " << width << " height=" << height << '\n';
  int max = 0;
  min = 10;
  for(int k=sy;k<height+sy;k++){
    int d1 = garden[k][sx];
    int d2 = garden[k][sx+width-1];
    if(d1>d2) d1 = d2;
    if(min>d1) min = d1;
  }
  for(int k=sx;k<width+sx;k++){
    int d1 = garden[sy][k];
    int d2 = garden[sy+height-1][k];
    if(d1>d2) d1=d2;
    if(min>d1) min = d1;
  }
     
  for(int i=sx+1;i<width+sx-1;i++){
    for(int j=sy+1;j<height+sy-1;j++){
      int d = garden[j][i];
      if(max<d) max = d;
    }
  }
     
  //std::cout << "  min=" << min << "  max=" << max << '\n';
     
  if(min>max) return true;
  else return false;
}
 
     
int main(){
  int m,n;
  while(1){
    int max = 0;
    std::cin >> m >> n;
    if(m==0&&n==0) break;
    for(int i=0;i<m;i++){
      for(int j=0;j<n;j++){
	std::cin >> garden[i][j];
      }
    }
    /*for(int i=0;i<m;i++){
      for(int j=0;j<n;j++){
      std::cout << garden[i][j] << ' ';
      }
      std::cout << '\n';
      }
    */
    for(int height=m;height>2;height--){
      for(int width=n;width>2;width--){
	for(int sy=0;sy<m-height+1;sy++){
	  for(int sx=0;sx<n-width+1;sx++){
	    if(isPond(sx,sy,width,height)){
	      int area = pond_area(sx,sy,width,height);
	      if(area > max) max = area;
	      //std::cout << "area=" << area << '\n';
	    }
	  }
	}
      }
    }
    std::cout << max << '\n';
  }
  return 0;
}