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)
}

Python: #!/usr/bin/env python3
# -*- coding: utf-8 -*-
import itertools

def rdp_connect() -> bool:
    global h, w, e
    h, w = map(int, input().split())
    if h == 0 and w == 0:
        return False
    e = [list(map(int, input().split())) for _ in range(h)]
    return True

def rdp_scan(x1: int, y1: int, x2: int, y2: int) -> int:
    edges = []
    edges.extend([e[j][x1] for j in range(y1, y2 + 1)])
    edges.extend([e[j][x2] for j in range(y1, y2 + 1)])
    edges.extend([e[y1][i] for i in range(x1 + 1, x2)])
    edges.extend([e[y2][i] for i in range(x1 + 1, x2)])
    inner = [e[j][i] for j, i in itertools.product(range(y1 + 1, y2), range(x1 + 1, x2))]
    edges_min = min(edges)
    if edges_min - max(inner) <= 0:
        return 0
    return sum(map(lambda e: edges_min - e, inner))

if __name__ == '__main__':
    while rdp_connect():
        best = 0
        for y1, x1 in itertools.product(range(h), range(w)):
            for y2, x2 in itertools.product(range(y1 + 2, h), range(x1 + 2, w)):
                best = max(best, rdp_scan(x1, y1, x2, y2))
        print(best)

Python: import sys
from itertools import permutations, combinations, product

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def LIST(): return list(map(int, input().split()))
def MAP(): return map(int, input().split())

ans = []
while 1:
	d, w = MAP()
	if d == 0 and w == 0:
		break
	e = [LIST() for _ in range(d)]
	sum_max = 0
	for i in combinations(range(d), 2):
		if abs(i[0]-i[1]) == 1:
			continue
		for j in combinations(range(w), 2):
			if abs(j[0]-j[1]) == 1:
				continue
			# 堀の最小値を見つける
			min_ = 10
			for k in range(i[0], i[1]+1):
				if k == i[0] or k == i[1]:
					for l in range(j[0], j[1]+1):
						min_ = min(min_, e[k][l])
				else:
					min_ = min(min_, e[k][j[0]])
					min_ = min(min_, e[k][j[1]])
			# 内側を調べる．内側にmin_以上の値があればだめ．
			sum_ = 0
			for k in range(i[0]+1, i[1]):
				for l in range(j[0]+1, j[1]):
					# print(k, l, e[k][l])
					if e[k][l] >= min_:
						sum_ = 0
						break
					else:
						sum_ += (min_-e[k][l])
				if e[k][l] >= min_:
					break
			sum_max = max(sum_max, sum_)

	ans.append(sum_max)
for x in ans:
	print(x)

Python: while 1:
    d, w = map(int, input().split())
    if d==w==0:
        break
    E=[list(map(int, input().split())) for i in range(d)]
    a=0
    for x2 in range(d):
        for y2 in range(w):
            for x1 in range(x2-1):
                for y1 in range(y2-1):
                    co=10**18
                    for x in range(x1, x2+1):
                        co=min(co, E[x][y1], E[x][y2])
                    for y in range(y1, y2+1):
                        co=min(co, E[x1][y], E[x2][y])
                    ci=cnt=0
                    for x in range(x1+1, x2):
                        for y in range(y1+1, y2):
                            ci=max(ci, E[x][y])
                            cnt+=E[x][y]
                    if ci < co:
                        a=max(ans, (x2-x1-1)*(y2-y1-1)*co - cnt)
    print(a)

Python: while True:
  d, w = map(int, input().split())
  if d == 0:break
  mp = [list(map(int, input().split())) for _ in range(d)]
  def solve():
    ans = 0
    for left in range(w - 1):
      for right in range(w - 1, left + 1, -1):
        for top in range(d - 1):
          for under in range(d - 1, top + 1, -1):
            frame_height = 10
            frame_height = min(frame_height, min(mp[top][left:right + 1]))
            frame_height = min(frame_height, min(mp[under][left:right + 1]))
            frame_height = min(frame_height, min(mp[i][left] for i in range(top, under)))
            frame_height = min(frame_height, min(mp[i][right] for i in range(top, under)))
            pond_height = 0
            pond_height = max(pond_height, max(max(mp[i][left + 1:right]) for i in range(top + 1, under)))
            if pond_height < frame_height:
              temp = frame_height * (under - top - 1) * (right - left - 1) - \
                    sum(sum(mp[i][left + 1:right]) for i in range(top + 1, under))
              ans = max(temp, ans)
    print(ans)
  
  solve()

Python: while 1:
    d, w = map(int, input().split())
    if d == w == 0:
        break
    E = [[*map(int, input().split())] for i in range(d)]
    ans = 0
    for x2 in range(d):
        for y2 in range(w):
            for x1 in range(x2-1):
                for y1 in range(y2-1):
                    co = 10**18
                    for x in range(x1, x2+1):
                        co = min(co, E[x][y1], E[x][y2])
                    for y in range(y1, y2+1):
                        co = min(co, E[x1][y], E[x2][y])
                    ci = 0
                    cnt = 0
                    for x in range(x1+1, x2):
                        for y in range(y1+1, y2):
                            ci = max(ci, E[x][y])
                            cnt += E[x][y]
                    if ci < co:
                        ans = max(ans, (x2-x1-1)*(y2-y1-1)*co - cnt)
    print(ans)