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>
#define LL long long


using namespace std;

int a[15][15],vis[15][15];

struct node
{
    int x,y;
    node(int x,int y):x(x),y(y){}
};

queue<node>q;
int n,m;

int main()
{
    std::ios::sync_with_stdio(false);
    while (cin>>n>>m)
    {
        if (n==0 && m==0) break;
        int ans=0;
        for (int i=1;i<=n;i++)
            for (int j=1;j<=m;j++)
                cin>>a[i][j];
        for (int i=1;i<=n;i++)
            for (int j=1;j<=m;j++)
            {
                for (int ii=n;ii>=i+2;ii--)
                    for (int jj=m;jj>=j+2;jj--)
                    {
                        int H=1e9;
                        for (int x=i;x<=ii;x++) H=min(a[x][j],min(a[x][jj],H));
                        for (int y=j;y<=jj;y++) H=min(a[i][y],min(a[ii][y],H));

                        int flg=1,res=0;
                        for (int x=i+1;x<ii;x++)
                            if (flg) for (int y=j+1;y<jj;y++)
                            {
                                if (a[x][y]>=H) {res=0;flg=0;break;}
                                else res=res+H-a[x][y];
                            }
                        ans=max(ans,res);
                    }
            }
        cout<<ans<<endl;
    }
    return 0;
}

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

using namespace std;

struct Pond
{
  int up,down,left,right;
  vector<int> canal;
  vector<int> moat;
  int min,max;
  void make(int up,int down,int left,int right,vector<int> canal,vector<int>moat)
  {
    this->up=up; this->down=down; this->left=left; this->right=right;
    this->canal=canal; this->moat=moat;
  }
  void setVals(){
    max= *max_element(canal.begin(),canal.end());
    min= *min_element(moat.begin(),moat.end());
  }
  bool isMake()
  {
    return (max<min);
  }
};

int calc(Pond pond)
{
  int sum=0;
  for(int i=0;i<pond.canal.size();i++){
    sum+=pond.min-pond.canal[i];
  }
  return sum;
}
void print(vector<int> list){
  for(int x:list) cout<<x<<" ";
  cout<<endl;
}
int main()
{
  int count=0;
  int depth,width;
  vector<int> canal(0),moat(0);
  cin>>depth>>width;
  int stage[depth][width]={{}};
  if((depth==0)and(width==0)) return 0;
  
  for(int i=0;i<depth;i++){
    for(int j=0;j<width;j++){
      cin>>stage[i][j];
    }
  }
  int mx=0;
  for(int up=0;up<depth-1;up++){
    for(int left=0;left<width-1;left++){
      for(int down=up+2;down<depth;down++){
	for(int right=left+2;right<width;right++){
	  //cout<<"up="<<up<<" down="<<down<<endl;
	  //cout<<"left="<<left<<" right="<<right<<endl;
	  canal.resize(0);
	  moat.resize(0);
	  for(int s=up;s<=down;s++){
	    for(int t=left;t<=right;t++){
	      //cout<<"s="<<s<<" t="<<t<<endl;
	      if((s!=up and s!=down)and(t!=left and t!=right)){
		//cout<<"canal.push("<<stage[s][t]<<")"<<endl;
		canal.push_back(stage[s][t]);
	      }else{
		//cout<<"moat.push("<<stage[s][t]<<")"<<endl;
		moat.push_back(stage[s][t]);
	      }
	    }
	  }
	  //print(canal);
	  //print(moat);
	  Pond pond;
	  count++;
	  pond.make(up,down,left,right,canal,moat);
	  pond.setVals();
	  //cout<<"make pond:"<<endl;
	  if(pond.isMake()){
	    //cout<<"judge=YES"<<endl;
	    mx=max(mx,calc(pond));
	  }else{
	    //cout<<"judge=NO"<<endl;
	  }
	}
      }
    }
  }
  //cout<<"count="<<count<<endl;
  cout<<mx<<endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define INF 1e9
int grid[20][20];
int d, w;
int calc(int top, int bottom, int left, int right) {
  int mi = INF;
  for (int i = left; i <= right; i++) {
    mi = min(grid[top][i], mi);
    mi = min(grid[bottom][i], mi);
  }
  for (int i = top; i <= bottom; i++) {
    mi = min(grid[i][left], mi);
    mi = min(grid[i][right], mi);
  }

  int ans = 0;
  for (int i = 0; i < bottom-top-1; i++) {
    for (int j = 0; j < right-left-1; j++) {
      if (mi <= grid[i+top+1][j+left+1]) {
	return -1;
      }
      ans += (mi-grid[i+top+1][j+left+1]);
    }
  }
  
  return ans;
}

signed main() {
  while (cin>>d>>w, d+w) {
    for (int i = 0; i < d; i++) {
      for (int j = 0; j < w; j++) {
	cin >> grid[i][j];
      }
    }
    int ans = -1;
    for (int i = 0; i < d; i++) {
      for (int j = d-1; j >= 0; j--) {
	for (int k = 0; k < w; k++) {
	  for (int l = w-1; l >= 0; l--) {
	    if (i == j || k == l) break;
	    ans = max(ans, calc(i, j, k, l));
	  }
	}
      }
    }
    cout << ans << endl;
  }
}