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("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using ull=unsigned long long int;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
const ll mod=1000000007;
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
ll lcm(ll c,ll d){return c/gcd(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
int main(){
while(1){
int n,m;
cin>>n>>m;
if(n==0)return 0;
V<V<int>> a(n,V<int>(m));
int ans=0;
for(int i=0;i<n;i++)for(int j=0;j<m;j++)cin>>a[i][j];
    for(int i=3;i<=10;i++){//縦の長さ
      for(int j=3;j<=10;j++){//横の長さ
      for(int k=0;k<n;k++){
        for(int l=0;l<m;l++){
          if(k+i>n||l+j>m)continue;
          int ma=INT_MAX;
          for(int i2=k;i2<k+i;i2++){//縦枠最小
            chmin(ma,a[i2][l]);
            chmin(ma,a[i2][l+j-1]);
          }
          for(int j2=l;j2<l+j;j2++){//横枠最小
            chmin(ma,a[k][j2]);
            chmin(ma,a[k+i-1][j2]);
          }
          bool c=true;int sum=0;
          for(int i2=k+1;i2<k+i-1;i2++){//内枠が水で満たせるか確認　及び最大容量を計算
            for(int j2=l+1;j2<l+j-1;j2++){
              if(ma<=a[i2][j2])c=false;
              else sum+=ma-a[i2][j2];
            }
          }
          if(c)chmax(ans,sum);
        }
      }
  }
}
cout<<ans<<"\n";
}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
#define R(i,a,n) for(int i=a;i<=n;i++)
int a[11][11],w,h;
int chp(int y1,int y2,int x1,int x2){
  int res=0,ans=0,t=1e9;
  R(i,y1,y2)R(j,x1,x2)res=max(res,a[i][j]);
  R(i,y1-1,y2+1)R(j,x1-1,x2+1)
    if(i==y1-1||i==y2+1||j==x1-1||j==x2+1){
      if(a[i][j]<=res)return 0;
      t=min(t,a[i][j]);
    }
  R(i,y1,y2)R(j,x1,x2)ans+=t-a[i][j];
  return ans;
}
int ch(int y,int x){
  int ans=0;
  R(i,y,h-2)R(j,x,w-2)ans=max(ans,chp(y,i,x,j));
  return ans;
}
main(){
  while(cin>>h>>w,h){
    int ans=0;
    r(i,h)r(j,w)cin>>a[i][j];
    r(i,h-1)r(j,w-1)if(i&&j)ans=max(ans,ch(i,j));
    cout<<ans<<endl;
  }
}

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

using namespace std;

int e[101][101];

int cal(int lx, int rx, int ly, int ry) {
    int ret = 0;
    int soto_min = 100;

    for (int i = lx; i <= rx; i++) {
        for (int j = ly; j <= ry; j++) {
            if (i == lx || i == rx || j == ly || j == ry) {
                soto_min = min(soto_min, e[j][i]);
            }
        }
    }

    for (int i = lx; i <= rx; i++) {
        for (int j = ly; j <= ry; j++) {
            if (i == lx || i == rx || j == ly || j == ry) {
                continue;
            }

            if (e[j][i] >= soto_min) { return 0; }

            ret += (soto_min - e[j][i]);
        }
    }
    return ret;
}

int main() {
    int d, w;

    while (cin >> d >> w) {
        if ( d == 0 && w == 0 ) { break; }

        for (int i = 0; i < d; i++) {
            for (int j = 0; j < w; j++) {
                cin >> e[i][j];
            }
        }
        int ans = 0;
        for (int lx = 0; lx < w-2; lx++) {
            for (int rx = lx+2; rx < w; rx++) {
                for (int ly = 0; ly < d-2; ly++) {
                    for (int ry = 0; ry < d; ry++) {
                        ans = max(ans, cal(lx, rx, ly, ry));
                    }
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}