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++: // g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
//#define int ll
typedef int Def;
//typedef ll Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
//template<class C>constexpr int size(const C &c){return (int)c.size();}
//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}
const double PI=acos(-1);
const double EPS=1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int main(){
	int n,m;
	while(cin>>n>>m,n){
		vvi in(n,vi(m));
		rep(i,n)rep(j,m)cin>>in[i][j];
		int out=0;
		rep(i,n)rep(j,m)rep(k,n)rep(l,m)if(i+1<k&&j+1<l){
			int mi=inf,sum=0,ma=-1;
			loop(x,i,k+1)loop(y,j,l+1){
				if(x==i||x==k||y==j||y==l)mi=min(mi,in[x][y]);
				else{
					ma=max(ma,in[x][y]);
					sum+=in[x][y];
				}
			}
			if(ma<mi)out=max(out,(k-i-1)*(l-j-1)*mi-sum);
		}
		cout<<out<<endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int d,w;
  while(cin>>d>>w,d||w){
    int e[d][w];
    for(int i=0;i<d;i++)
      for(int j=0;j<w;j++)
	cin>>e[i][j];
    int ans=0;
    for(int i=0;i<d;i++){
      for(int j=0;j<w;j++){
	for(int k=i+2;k<d;k++){
	  for(int l=j+2;l<w;l++){
	    int lb=0;
	    for(int a=i+1;a<k;a++){
	      for(int b=j+1;b<l;b++){
		lb=max(lb,e[a][b]);
	      }
	    }
	    int ub=e[i][j];
	    for(int a=j;a<=l;a++){
	      ub=min(ub,e[i][a]);
	      ub=min(ub,e[k][a]);
	    }
	    for(int b=i;b<=k;b++){
	      ub=min(ub,e[b][j]);
	      ub=min(ub,e[b][l]);
	    }
	    if(lb>=ub) continue;
	    int tmp=0;
	    for(int a=i+1;a<k;a++){
	      for(int b=j+1;b<l;b++){
		tmp+=ub-e[a][b];
	      }
	    }
	    //cout<<lb<<" "<<ub<<":"<<tmp<<endl;
	    ans=max(ans,tmp);
	  }
	}
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <sstream>

int main () {
  int x;
  while(1){
    int d,w;
    x=0;
    std::cin>>d>>w;
    if(d==0 and w==0){
      break;
    }
    int y[d][w];
    for(int i=0;i<d;i++){
      for(int j=0;j<w;j++){
	std::cin>>y[i][j];
      }
    }

    for(int i=0;i<d-2;i++){
      for(int j=i+2;j<d;j++){
	for(int k=0;k<w-2;k++){
	  for(int l=k+2;l<w;l++){
	    int min=10,in=0,z=0;
	    for(int m=i;m<j+1;m++){
	      for(int n=k;n<l+1;n++){
		if(m==i or m==j or n==k or n==l){
		  if(y[m][n]<min){
		    min=y[m][n];
		  }
		}
	      }
	    }
	    int max=0;
	    for(int m=i+1;m<j;m++){
	      for(int n=k+1;n<l;n++){
		if(y[m][n]>max){
		  max=y[m][n];
		}
		in+=y[m][n];
		z++;
	      }
	    }
	    int tmp;
	    if(max<min and (tmp=(min*z-in))>x){
	      x=tmp;
	    }
	  }
	}
      }
    }
    std::cout<<x<<"\n";
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1<<30)
#define INFLL (1ll<<60)
typedef pair<int, int> P;
typedef pair<int, P> E;
#define MOD (1000000007ll)
#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	b += MOD;
	a += b;
	a %= MOD;
}

int main(void){
	int h,w,e[12][12],i,j,l,r,t,b,d,tmp,ans=0;
	bool flag = false;
	while(true){
		cin >> h >> w;
		if(!(h&&w)){
			break;
		}
		ans = 0;
		for(i=0; i<h; ++i){
			for(j=0; j<w; ++j){
				cin >> e[i][j];
			}
		}
		for(t=0; t<h; ++t){
			for(b=t+2; b<h; ++b){
				for(l=0; l<w; ++l){
					for(r=l+2; r<w; ++r){
						d = INF;
						for(i=t; i<=b; ++i){
							d = min(d,e[i][l]);
							d = min(d,e[i][r]);
						}
						for(j=l; j<=r; ++j){
							d = min(d,e[t][j]);
							d = min(d,e[b][j]);
						}
						tmp = 0;
						flag = true;
						for(i=t+1; i<b; ++i){
							for(j=l+1; j<r; ++j){
								if(e[i][j]>=d){
									flag = false;
									break;
								}
								tmp += (d-e[i][j]);
							}
							if(!flag){
								break;
							}
						}
						if(flag){
							ans = max(ans,tmp);
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}