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++: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define all(x) x.begin(),x.end()
#define ln '\n'
const long double PI = acos(-1.0L);
const long long MOD = 1000000007LL;
//const long long MOD = 998244353LL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true;} return false; }
///////////////////////////////////////////////////////////////////////////////////////////////////

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    while (1) {
        int H,W; cin >> H >> W;
        if (H+W==0) break;
        vector<vector<int>> S(H, vector<int>(W));
        rep(i,H) rep(j,W) cin >> S[i][j];

        int ans = 0;
        rep(i,H) {
            rep(j,W) {
                for (int k = i+2; k < H; ++k) {
                    for (int l = j+2; l < W; ++l) {
                        int wall = 1e9;
                        for (int m = i; m <= k; ++m) {
                            chmin(wall,S[m][j]);
                            chmin(wall,S[m][l]);
                        }
                        for (int m = j; m <= l; ++m) {
                            chmin(wall,S[i][m]);
                            chmin(wall,S[k][m]);
                        }
                        bool ok = true;
                        int val = 0;
                        for (int m = i+1; m < k; ++m) {
                            for (int n = j+1; n < l; ++n) {
                                if (S[m][n] >= wall) ok = false;
                                val += wall - S[m][n];
                            }
                        }
                        if (ok) chmax(ans,val);
                    }
                }
            }
        }

        cout << ans << ln;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef string::const_iterator State;
const int MOD=1000000007;
const int INF=1000000000;
typedef long long ll;
typedef vector<int> vi;
const double eps=1e-9;
const int inf=1e9;
typedef pair<int,int> P;
int d,w;
int H,W;
int board[15][15];
int ans=0;
bool ok;
int tmp=0;
int mxinner=0;
int miwall=20;
bool wallout(int y,int x,int wall)
{
  if(board[y][x]<wall) return true;
  else return false;
}
void inner(int uly,int ulx,int lry,int lrx)
{
  for(int y=uly;y<=lry;y++)
    {
      for(int x=ulx;x<=lrx;x++)
	{
	  mxinner=max(mxinner,board[y][x]);
	}
    }
}
int waterfell(int uly,int ulx,int lry,int lrx,int water)
{
  int tmp=0;
  for(int y=uly;y<=lry;y++)
    {
      for(int x=ulx;x<=lrx;x++)
	{
	  tmp+=(water-board[y][x]);
	}
    }
  return tmp;
}
void outer(int uly,int ulx,int lry,int lrx)
{
  uly--,ulx--,lry++,lrx++;
  for(int y=uly;y<=lry;y++)
    {
      miwall=min(miwall,board[y][ulx]);
    }
  for(int x=ulx;x<=lrx;x++)
    {
      miwall=min(miwall,board[uly][x]);
    }
  for(int y=uly;y<=lry;y++)
    {
      miwall=min(miwall,board[y][lrx]);
    }
  for(int x=ulx;x<=lrx;x++)
    {
      miwall=min(miwall,board[lry][x]);
    }
}
int main(int argc,char const* argv[])
{
  while(cin >> d >> w && d)
    {
      for(int i=0;i<d;i++)
	{
	  for(int j=0;j<w;j++)
	    {
	      cin >> board[i][j];
	    }
	}
      ans=0;
      for(int uly=1;uly<d-1;uly++)
	{
	  for(int ulx=1;ulx<w-1;ulx++)
	    {
	      for(int lry=uly;lry<d-1;lry++)
		{
		  for(int lrx=ulx;lrx<w-1;lrx++)
		    {
		      miwall=20;
		      mxinner=0;
		      inner(uly,ulx,lry,lrx);
		      outer(uly,ulx,lry,lrx);
		      int water=miwall;
		      if(miwall<=mxinner) continue;
		      else
			{
			  ans=max(ans,waterfell(uly,ulx,lry,lrx,water));
			}
		    }
		}
	    }
	}
      cout << ans << endl;
    }
  return 0;
}