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 repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 1120000000

int main(){
  int d,w;
  while(cin>>d>>w, d|w){
    vector<vector<int>> v(d,vector<int>(w));
    rep(i,d)rep(j,w) cin>>v[i][j];

    int ans = 0;
    rep(i,d)rep(j,w)repl(k,i+2,d)repl(l,j+2,w){
      int mn = INF;
      for(int x=j; x<=l; x++) mn = min(mn, v[i][x]);
      for(int x=j; x<=l; x++) mn = min(mn, v[k][x]);
      for(int x=i; x<=k; x++) mn = min(mn, v[x][j]);
      for(int x=i; x<=k; x++) mn = min(mn, v[x][l]);

      int tmp = 0;
      repl(ii,i+1,k)repl(jj,j+1,l){
        if(v[ii][jj]>=mn) tmp = -INF;
        else tmp += mn - v[ii][jj];
      }
      ans = max(ans, tmp);
    }

    cout << ans << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int d,w;
int f[100][100];
int valid(int x,int y,int dx,int dy){
	int e = 1000000000;
	int in = 0;
	int res = 0;
	for(int i=0;i<dx;i++){
		e = min(e,f[x+i][y]);
		e = min(e,f[x+i][y+dy-1]);
	}
	for(int i=0;i<dy;i++){
		e = min(e,f[x][y+i]);
		e = min(e,f[x+dx-1][y+i]);
	}
	for(int i=1;i<dx-1;i++){
		for(int j=1;j<dy-1;j++){
			in = max(in,f[x+i][y+j]);
		}
	}
	if(in >= e)return -1;
	for(int i=1;i<dx-1;i++){
		for(int j=1;j<dy-1;j++){
			res += e-f[x+i][y+j];
		}
	}
	return res;
}
int sol(int x ,int y){
	int res = 0;	
	for(int i=3;x+i<=d;i++){
		for(int j=3;y+j<=w;j++){
			res = max(res,valid(x,y,i,j));
		}
	}
	return res;
}
bool solve(){
	cin >> d >> w;
	if(d+w==0)return false;
	for(int i=0;i<d;i++){
		for(int j=0;j<w;j++){
			cin >> f[i][j];
		}
	}
	int ans = 0;
	for(int i=0;i<d;i++){
		for(int j=0;j<w;j++){
			ans = max(ans,sol(i,j));
		}
	}
	cout << ans << endl;
	return true;
}
int main(){
	while(solve()){}
	return 0;
}

C++: #include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define FOR(i,a,b) for (int i=(a),for_##i##_max=(b);i<=(for_##i##_max);++i)
#define RFOR(i,a,b) for (int i=(a),rfor_##i##_min=(b);i>=(rfor_##i##_min);--i)
#define REP(i,n) for (int i=0,rep_##i##_len=(n);i<(rep_##i##_len);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)

namespace { // Basics

template<typename T>
void chmin(T &x, T y) {
  x = std::min(x, y);
}

template<typename T>
void chmax(T &x, T y) {
  x = std::max(x, y);
}

template<typename T>
__attribute__((unused))
const auto minimum = static_cast<T const &(*)(T const &, T const &)>(std::min);

template<typename T>
__attribute__((unused))
const auto maximum = static_cast<T const &(*)(T const &, T const &)>(std::max);

template<typename T>
T constexpr infty = std::numeric_limits<T>::max / 3;
template<>
auto constexpr infty<int> = 1'100'100'100;
template<>
__attribute__((unused))
auto constexpr infty<long long int> = 100'500'400'300'200'100LL;

using lli = long long int;
using ld = long double;

} // namespace

using namespace std;

int main() {
  // cout << fixed << setprecision(10);

  int D, W;
  while (cin >> D >> W && D) {
    vector<vector<int>> garden(D, vector<int>(W));
    REP(i, D) REP(j, W) {
      cin >> garden[i][j];
    }
    int ans = 0;
    REP(is, D) REP(js, W) REP(ie, D) REP(je, W) {
      if (is + 1 >= ie || js + 1 >= je) { continue; }
      // cerr << stringify(is, js, ie, je) << "\n";
      int wall_height = infty<int>;
      FOR(x, is, ie) {
        chmin(wall_height, garden[x][js]);
        chmin(wall_height, garden[x][je]);
      }
      FOR(y, js, je) {
        chmin(wall_height, garden[is][y]);
        chmin(wall_height, garden[ie][y]);
      }
      // cerr << "wall_height: " << wall_height << "\n";
      int pond_height = -1;
      int water = 0;
      FOR(x, is+1, ie-1) FOR(y, js+1, je-1) {
        // cerr << stringify(x, y) << "\n";
        int const h = garden[x][y];
        chmax(pond_height, h);
        water += wall_height - h;
      }
      if (pond_height >= wall_height) { continue; }
      chmax(ans, water);
    }
    cout << ans << endl;
  }
  return 0;
}