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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(long long i=0;i<n;++i)
#define REPR(i,n) for(long long i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair
#define SV(v) {for(long long sitr=0;sitr<v.size();++sitr){cin>>v[sitr];}}

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

typedef pair<int,int> pii;

int main(){

  int n, m;
  int mass[10][10];
  while(true){
    cin >> n >> m;
    if(n == 0 && m == 0) break;

    REP(i,n){
      REP(j,m){
        cin >> mass[i][j];
      }
    }

    llong ans = 0;
    REP(y1,n){
      for(int y2 = y1+1; y2 < n; ++y2){
        REP(x1,m){
          for(int x2 = x1+1; x2 < m; ++x2){
            
            int mx = INF;
            for(int i = y1; i <= y2; ++i){
              mx = min({mx, mass[i][x1], mass[i][x2]});
            }
            for(int i = x1; i <= x2; ++i){
              mx = min({mx, mass[y1][i], mass[y2][i]});
            }

            llong sum = 0;
            bool pond = true;
            for(int i = x1+1; i < x2; ++i){
              for(int j = y1+1; j < y2; ++j){
                if(mass[j][i] >= mx){
                  pond = false;
                  break;
                }
                sum += mx - mass[j][i];
              }
              if(!pond){
                break;
              }
            }

            if(pond){
              ans = max(ans, sum);
            }

          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;

#define MAX 12
#define INF 100
int main(){
  
  while(true){
    int d, w, ans = 0;
    int e[MAX][MAX];
    
    cin>>d>>w;

    if(!d &&!w) break;

    for(int i = 0; i < d; i++){
      for(int j = 0; j < w; j++){
	cin>>e[i][j];
      }
    }
    

    for(int i = 1; i < d-1; i++){
      for(int j  = 1; j < w-1; j++){
	for(int k = i; k < d-1; k++){
	  for(int l = j; l < w-1; l++){
	    int min_ = INF, sum = 0;
	    bool flag = false;
	    for(int m = i-1; m <= k+1; m++){
	      //cout<<m<<" ";
	      min_ = min(min_, e[m][j-1]);
	      min_ = min(min_, e[m][l+1]);
	    }//cout<<endl;

	    for(int m = j-1; m <= l+1; m++){
	      // cout<<m<<" ";
	      min_ = min(min_, e[i-1][m]);
	      min_ = min(min_, e[k+1][m]);
	    }//cout<<endl;
	    //cout<<min_<<endl;
	    for(int m = i; m <= k; m++){
	      for(int n = j; n <= l; n++){
		if(min_ <= e[m][n]){
		  flag = true;
		  break;
		} else {
		  sum += min_ - e[m][n];
		}
	      }
	      if(flag) break;
	    }
	    //cout<<sum<<endl;
	    if(!flag) ans = max(ans, sum);
	    
	  }
	}
      }
    }

    cout<<ans<<endl;
    
  }
}

C++: #include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-11;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
const int DX[8]={-1,0,1,0,-1,-1,1,1},DY[8]={0,-1,0,1,-1,1,-1,1};


int main(){;
	while(1){
		int h,w;
		cin>>h>>w;
		if(h==0) break;
		vvi a(h,vi(w));
		for(int i=0;i<h;i++) for(int j=0;j<w;j++) cin>>a[i][j];
		int res=0;
		for(int lx=0;lx<h;lx++) for(int rx=lx+3;rx<=h;rx++) for(int ly=0;ly<w;ly++) for(int ry=ly+3;ry<=w;ry++){
			int mx=inf;
			for(int i=lx;i<rx;i++) for(int j=ly;j<ry;j++) if(i==lx||i==rx-1||j==ly||j==ry-1){
				mx=min(mx,a[i][j]);
			}
			bool B=1;
			for(int i=lx+1;i<rx-1;i++) for(int j=ly+1;j<ry-1;j++) if(a[i][j]>=mx) B=0;
			if(!B) continue;
			int tmp=0;
			for(int i=lx+1;i<rx-1;i++) for(int j=ly+1;j<ry-1;j++) tmp+=mx-a[i][j];
			res=max(res,tmp);
		}
		cout<<res<<endl;
	}
}