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>

int main() {
  int depth, width;
  int map[10][10];

  //reading a given map
  std::cin >> depth >> width;

  while(depth != 0 && width != 0) {
    //map initialization
    for(int i=0; i<10; i++)
      for(int j=0; j<10; j++) {
        map[i][j] = -1;
    }

    for(int i=0; i<depth; i++)
      for(int j=0; j<width; j++) {
        std::cin >> map[i][j];
    }

    int max_cap = 0;
    for(int level=1; level<10; level++)
      for(int i=1; i<depth-1; i++)
        for(int j=1; j<width-1; j++) {
          if(map[i][j] < level && map[i-1][j] >= level && map[i][j-1] >= level && map[i-1][j-1] >= level) {
            int i_start=i, j_start=j, i_start2, j_start2, i_end, j_end;
            int ii, jj;
            int flag = 0;

            //std::cout << "level:" << level << " i_s:" << i_start << " j_s:"<< j_start << std::endl;

            for(jj=j_start; map[i_start][jj+1] < level; jj++) {
              if(map[i_start-1][jj] < level || jj==width-1) { flag = 1; break; }
            }
            if(flag==1 || map[i_start-1][jj] < level || map[i_start-1][jj+1] < level) { continue; }
            else { j_end = jj; }
            //std::cout << "j_e:" << j_end << std::endl;

            for(ii=i_start; map[ii+1][j_end] < level; ii++) {
              if(map[ii][j_end+1] < level || ii==depth-2) { flag = 1; break; }
            }
            if(flag==1 || map[ii][j_end+1] < level || map[ii+1][j_end+1] < level) { continue; }
            else { i_end = ii; }
            //std::cout << "i_e:" << i_end << std::endl;

            for(jj=j_end; map[i_end][jj-1] < level; jj--) {
              if(map[i_end+1][jj] < level || jj==1) { flag = 1; break; }
            }
            if(flag==1 || map[i_end+1][jj] < level || map[i_end+1][jj-1] < level) { continue; }
            else { j_start2 = jj; }
            //std::cout << "j_s2:" << j_start2 << std::endl;

            for(ii=i_end; map[ii-1][j_start2] < level; ii--) {
              if(map[ii][j_start2-1] < level || ii==1) { flag = 1; break; }
            }
            if(flag==1 || map[ii][j_start2-1] < level || map[ii-1][j_start2-1] < level) { continue; }
            else { i_start2 = ii; }
            //std::cout << "i_s2:" << i_start2 << std::endl;

            if(i_start != i_start2 || j_start != j_start2) { continue; }

            int cap = 0;
            for(ii=i_start; ii<=i_end; ii++) {
              for(jj=j_start; jj<=j_end; jj++) {
                if(map[ii][jj] >= level) { flag = 1; break; }
                cap = cap + (level - map[ii][jj]);
              }
              if(flag==1) { break; }
            }
            if(flag==1) { continue; }

            if(max_cap < cap) max_cap = cap;
          }
    }

    std::cout << max_cap << std::endl;

    std::cin >> depth >> width;
  }

  return 0;
}

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

int a[100][100];
bool used[100][100];
int dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};
int d,w,m;

int check(int x,int y){
  int res = 114514;
  for(int i = -1; i <= 1; i++){
    for(int j = -1; j <= 1; j++){
      if(i == 0 && j == 0) continue;
      if(used[x+i][y+j]) continue;
      res = min(res,a[x+i][y+j]);
    }
  }
  return res;
}

int dfs(int x,int y){
  used[x][y] = 1;
  m = max(m,a[x][y]);
  for(int i = 0; i < 4; i++){
    int nx = x+dx[i];
    int ny = y+dy[i];
    if(used[nx][ny]) continue;
    if((x == 0 || x == d-1) || (y == 0 || y == w-1)) continue;
    if(check(nx,ny) > check(x,y)) dfs(nx,ny);
  }

  int res = 0;
  for(int i = 0; i < d; i++){
    for(int j = 0; j < w; j++){
      if(used[i][j]) res += (m-a[i][j]);
    }
  }
  return res;
}

signed main(){

  while(1){
    cin >> d >> w;
    if(d == 0) break;
    for(int i = 0; i < d; i++){
      for(int j = 0; j < w; j++){
        cin >> a[i][j];
      }
    }

    int ans = 0;
    for(int i = 1; i < d-1; i++){
      for(int j = 1; j < w-1; j++){
        fill(used[0],used[100],0);
        m = 0;
        ans = max(ans,dfs(i,j));
      }
    }
    cout << ans << endl;
  }

  return 0;
}