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 a[15][15] ;
int fx[8] = { -1 , 0 , 1 , 0 , -1 , -1 , 1 , 1 } ;
int fy[8] = { 0 , 1 , 0 , -1 , -1 , 1 , -1 , 1 } ;
bool vis[15][15] ;
queue< pair< int , int > > q ;
set< int > seto ;
int bcj[110] ;
int calc( int x , int y ){
    return 10 * ( x - 1 ) + y ;
}
int father( int x ){
    return bcj[x] == x ? x : bcj[x] = father( bcj[x] ) ;
}
void join( int x , int y ){
     int fx = father( x ) ;
     int fy = father( y ) ;
     if ( fx != fy ) bcj[fy] = fx ;
}
int main(){
    int d , w ;
    while( 1 ){
          scanf( "%d%d" , &d , &w ) ;
          if ( d == 0 && w == 0 ) break ;
          for( int i = 1 ; i <= d ; i++ )
              for( int j = 1 ; j <= w ; j++ )
                  scanf( "%d" , &a[i][j] ) ;
          int ans = 0 ;
          for( int i = 2 ; i <= d - 1 ; i++ )
              for( int j = 2 ; j <= w - 1 ; j++ ){
                  int val = a[i][j] ;
                  memset( vis , false , sizeof( vis ) ) ;
                  vis[i][j] = true ; while( !q.empty() ) q.pop() ;
                  q.push( make_pair( i , j ) ) ;
                  bool flag = true ;
                  while( !q.empty() ){
                        int x = q.front().first ;
                        int y = q.front().second ;
                        q.pop() ;
                        if ( x == 1 || x == d || y == 1 || y == w ) flag = false ;
                        for( int ii = 0 ; ii < 4 ; ii++ ){
                            int xx = x + fx[ii] ;
                            int yy = y + fy[ii] ;
                            if ( xx >= 1 && xx <= d && yy >= 1 && yy <= w && !vis[xx][yy] && a[xx][yy] <= val ){
                                vis[xx][yy] = true ; q.push( make_pair( xx , yy ) ) ;
                            }
                        }
                  }
                  if ( !flag ) continue ;
                  for( int ii = 1 ; ii <= 100 ; ii++ )
                      bcj[ii] = ii ;
                  for( int ii = 1 ; ii <= d ; ii++ )
                      for( int jj = 1 ; jj <= w ; jj++ ){
                          if ( !vis[ii][jj] ){
                              for( int kk = 0 ; kk < 4 ; kk++ ){
                                  int xx = ii + fx[kk] ;
                                  int yy = jj + fy[kk] ;
                                  if ( xx >= 1 && xx <= d && yy >= 1 && yy <= w && !vis[xx][yy] ){
                                      join( calc( ii , jj ) , calc( xx , yy ) ) ;
                                  }
                              }
                          }
                      }
                  seto.clear() ;
                  int sum = 0 ;
                  int cnt = 0 ;
                  for( int ii = 1 ; ii <= d ; ii++ )
                      for( int jj = 1 ; jj <= w ; jj++ ){
                          if ( !vis[ii][jj] ){
                              int fa = father( calc( ii , jj ) ) ;
                              seto.insert( fa ) ;
                          }
                          else{
                               cnt++ ;
                               sum += a[ii][jj] ;
                          }
                  }
                  if ( seto.size() != 1 ) continue ;
                  int minx = 10086 ;
                  int aa , bb , cc , dd ;
                  aa = cc = 10086 ;
                  bb = dd = -10086 ;
                  for( int ii = 1 ; ii <= d ; ii++ )
                      for( int jj = 1 ; jj <= w ; jj++ ){
                          if ( vis[ii][jj] ){
                              aa = min( aa , ii ) ;
                              bb = max( bb , ii ) ;
                              cc = min( cc , jj ) ;
                              dd = max( dd , jj ) ;
                              for( int kk = 0 ; kk < 8 ; kk++ ){
                                  int xx = ii + fx[kk] ;
                                  int yy = jj + fy[kk] ;
                                  if ( xx >= 1 && xx <= d && yy >= 1 && yy <= w && !vis[xx][yy] ){
                                      minx = min( minx , a[xx][yy] ) ;
                                  }
                              }
                          }
                      }
                  flag = true ;
                  for( int ii = aa ; ii <= bb ; ii++ )
                      for( int jj = cc ; jj <= dd ; jj++ )
                          if ( !vis[ii][jj] ) flag = false ;
                  if ( minx > val && flag ){
                      int res = cnt * minx - sum ;
                      ans = max( ans , res ) ;
                  }
              }
          printf( "%d\n" , ans ) ;
    }
    return 0 ;
}