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>
using namespace std;

int e[100][100];

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

  int ans=0;

  for(int x=0;x<w;x++){
    for(int y=0;y<d;y++){
      for(int xx=x+2;xx<w;xx++){
        for(int yy=y+2;yy<d;yy++){
          int mi=10;
          for(int i=y;i<=yy;i++){
            for(int j=x;j<=xx;j++){
              
              if(y<i&&i<yy&&x<j&&j<xx) continue;
              mi=min(mi,e[i][j]);
            }
          }
          int ma=0;
          for(int i=y+1;i<yy;i++){
            for(int j=x+1;j<xx;j++){
                ma=max(ma,e[i][j]);
            }
          }
          int res=0;
          if(mi-ma<=0) continue;
          for(int i=y+1;i<yy;i++){
            for(int j=x+1;j<xx;j++){
              res+=mi-e[i][j];
            }
          }
          ans=max(ans,res);
        }
      }
      
    }
  }

  cout<<ans<<endl;
}


main(){
  int d,w;
  while(true){
    cin>>d>>w;
    if(d==0) break;
    f(d,w);
  }
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
using namespace std;
int d,w;
int garden[10][10];

int  serchMinMax(int xmin,int xmax,int ymin,int ymax){
  //int maxD = -1;
  int minD = 10;
  int depth = 0;
  // deside max and min-----
  for(int i = ymin;i<=ymax; i+= (ymax-ymin) ){
    for(int j = xmin ; j <= xmax; j++){
      //maxD = max(garden[i][j] ,maxD);
      minD = min(garden[i][j],minD);
    }
  }
  for(int i = ymin+1; i < ymax; i++){
    for(int j = xmin; j <= xmax; j+= (xmax - xmin) ){
      //maxD = max(garden[i][j] ,maxD);
      minD = min(garden[i][j],minD);
    }
  }
  //cout << "max" << maxD << endl;
  //cout << "min" << minD << endl;
  // serch Pond depth

  for(int i= ymin + 1; i < ymax ; i++){
    for(int j = xmin + 1; j < xmax; j++){
      if(minD > garden[i][j]) depth +=  minD - garden[i][j] ;
      else return 0;
    }
  }
  return depth;
}

int main(){

  while(true){
    int ans = 0;
    scanf("%d %d",&d,&w);
    if(d == 0 && w == 0) break;
    //--------------------------input
    for(int i = 0; i < d; i ++){
      for(int j=0; j < w; j++){
        scanf("%d",&garden[i][j]);
      }
    }

    // select xmin ymin xmax ymax
    for(int i= 0; i <= w - 3; i++){//xmin
      for(int j = i+2; j < w; j++){//xmax
        for(int k = 0; k <= d - 3; k++){//ymin
          for(int l = k+2; l < d; l++){//ymax
            ans = max(ans,serchMinMax(i, j, k, l));
            //cout << "hi" << endl;
          }
        }
      }
    }
    //cout << "hi" << endl;
    //int x = serchMinMax(0, 3, 0, 2);
    //cout << x << endl;
    cout << ans << endl;

  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
using namespace std;
typedef long long ll;

int main() {
    int d, w;
    while (cin >> d >> w, d) {
        vector<vector<int>> e(d, vector<int>(w));
        for (int i=0; i<d; ++i)
            for (int j=0; j<w; ++j)
                cin >> e[i][j];

        int cap = 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 mn = INT_MAX, mx = 0, sum = 0;
                for (int y=i; y<=k; ++y) {
                    for (int x=j; x<=l; ++x) {
                        if (x == j || x == l || y == i || y == k) {
                            mn = min(mn, e[y][x]);
                        } else {
                            mx = max(mx, e[y][x]);
                            sum += e[y][x];
                        }
                    }
                }
                if (mn <= mx) continue;
                cap = max(cap, mn * (k-i-1) * (l-j-1) - sum);
            }

        cout << cap << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

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

int main() {
  int d, w;
  while ( cin >> d >> w, d ) {
    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 a = 1; a <= 9; a++ ) {
      for ( int i = 1; i < d-1; i++ ) {
	for ( int j = 1; j < w-1; j++ ) {
	  if ( e[i][j] >= a ) continue;
	  int y = i, x = j;
	  while ( y+1 < d-1 && e[y+1][j] < a ) y++;
	  while ( x+1 < w-1 && e[i][x+1] < a ) x++;
	  bool flag = true;
	  int sum = 0;
	  // if ( a == 2 ) cout << i << j << y << x << endl;
	  for ( int r = i; r <= y; r++ ) {
	    for ( int c = j; c <= x; c++ ) {
	      sum += a-e[r][c];	      
	      for ( int k = 0; k < 8; k++ ) {	      
		int ny = r+dy[k], nx = c+dx[k];
		if ( k == 0 && e[ny][nx] >= a ) {		  
		  flag = false;
		  r = y; c = x; break;		  
		}
		if ( (ny < i || y < ny || nx < j || x < nx) && e[ny][nx] < a ) {
		  flag = false;
		  r = y; c = x; break;		  
		}
	      }	      
	    }
	  }
	  if ( flag ) {
	    ans = max(ans, sum);
	    // if ( sum == 22 ) cout << i << " " << j << " " << a << endl;
	  }	  
	}
      }
    }

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