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

int d, w;
int cap = 0;
int e[10][10];

int hight_of_ike(int sx, int sy, int col, int row) {
    int suii = e[sy][sx];
    for (int y = sy; y <= sy + col; ++y) {
        for (int x = sx; x <= sx + row; ++x) {
            suii = max(suii, e[y][x]);
        }
    }
    return suii;
}

int hight_of_sto(int sx, int sy, int col, int row) {
    int hight = e[sx][sy];
    for (int y = sy; y <= sy + col; ++y) {
        for (int x = sx; x <= sx + row; ++x) {
            if (y > sy && y < sy + col && x > sx && x < sx + row) continue;
            hight = min(hight, e[y][x]);
        }
    }
    return hight;
}

int get_cap(int sto, int sx, int sy, int col, int row) {
    int tmp = 0;
    for (int y = sy; y <= sy + col; ++y) {
        for (int x = sx; x <= sx + row; ++x) {
            tmp += sto - e[y][x];
        }
    }
    return tmp;
}

int main() {
    while(cin >> d >> w, d||w) {
        cap = 0;
        for (int y = 0; y < d; ++y) {
            for (int x = 0; x < w; ++x) {
                cin >> e[y][x];
            }
        }
        for (int sy = 1; sy < d - 1; ++sy) {
            for (int sx = 1; sx < w - 1; ++sx) {
                for (int col = 0; sy + col < d - 1; ++col) {
                    for (int row = 0; sx + row < w - 1; ++row) {
                        int ike = hight_of_ike(sx, sy, col, row);
                        int sto = hight_of_sto(sx-1, sy-1, col+2, row+2);
                        if (sto - ike > 0) {
                            cap = max(cap, get_cap(sto, sx, sy, col, row));
                        }
                    }
                }
            }
        }
        cout << cap << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int INF = 10000000;
int main(){
  while (1){
    int d, w;
    cin >> d >> w;
    if (d == 0 && w == 0){
      break;
    }
    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 ans = 0;
    for (int i = 0; i < d; i++){
      for (int j = i + 2; j < d; j++){
        for (int k = 0; k < w; k++){
          for (int l = k + 2; l < w; l++){
            int mn = INF;
            for (int y = i; y <= j; y++){
              for (int x = k; x <= l; x++){
                if (y == i || y == j || x == k || x == l){
                  mn = min(mn, e[y][x]);
                }
              }
            }
            bool ok = true;
            int sum = 0;
            for (int y = i + 1; y <= j - 1; y++){
              for (int x = k + 1; x <= l - 1; x++){
                if (e[y][x] >= mn){
                  ok = false;
                } else {
                  sum += mn - e[y][x];
                }
              }
            }
            if (ok){
              ans = max(ans, sum);
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}

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

int d, w, e[10][10];

signed main(){
  
  while(1){
  
    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 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 minv = 1000;
	    int maxv = 0;
	    int sum = 0;
	    int cnt = 0;
	  
	    for(int y=i;y<=k;y++){
	    
	      for(int x=j;x<=l;x++){
	      
		if( y == i || y == k || x == j || x == l ) minv = min( minv, e[y][x] );
		else{
		  maxv = max( maxv, e[y][x] );
		  sum += e[y][x];
		  cnt++;
		}
	      
	      }
	    
	    }
	  
	    if( minv > maxv ) ans = max( ans, minv * cnt - sum );
	  
	  }
	
	}
      
      }
    
    }
  
    cout<<ans<<endl;

  }
  
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  while (true) {
    int h, w;
    cin >> h >> w;
    if (h == 0)
      break;
    vector<vector<int>> mp(h, vector<int>(w));
    int ans = 0;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> mp[i][j];
    for (int i = 0; i < h - 2; i++)
      for (int j = 0; j < w - 2; j++) {
        for (int n = 3; n <= h - i; n++)
          for (int m = 3; m <= w - j; m++) {
            int mn = 100;
            bool ok = true;
            for (int k = 0; k < n; k++)
              mn = min(mn, mp[i + k][j]);
            for (int k = 0; k < n; k++)
              mn = min(mn, mp[i + k][j + m - 1]);
            for (int k = 0; k < m; k++)
              mn = min(mn, mp[i][j + k]);
            for (int k = 0; k < m; k++)
              mn = min(mn, mp[i + n - 1][j + k]);
            int sum = (n - 2) * (m - 2) * mn;
            for (int k = 1; k < n - 1; k++)
              for (int l = 1; l < m - 1; l++) {
                if (mn <= mp[i + k][j + l])
                  ok = false;
                sum -= mp[i + k][j + l];
              }
            if (ok)
              ans = max(ans, sum);
          }
      }
    cout << ans << endl;
  }
  return 0;
}