#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, a[502][502], l, r, x, y, sum = 0;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] != 0) continue;
      r = min(a[i][j + 1] - 1, a[i + 1][j] - 1);
      x = i, y = j;
      while (a[i][y] == 0) y--;
      while (a[x][j] == 0) x--;
      l = min(j - y + a[i][y], i - x + a[x][j]);
      if (l >= a[i][j + 1] or l >= a[i + 1][j] or l > r or
          a[i][y] + j - y >= a[i][j + 1] or a[x][j] >= a[i + 1][j]) {
        cout << -1;
        return 0;
      }
      a[i][j] = r;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 1; j < m; j++) {
      if ((j - 1 > 0 and a[i][j] <= a[i][j - 1]) or
          (j + 1 < m and a[i][j] >= a[i][j + 1]) or
          (i - 1 > 0 and a[i][j] <= a[i - 1][j]) or
          (i + 1 < n and a[i][j] >= a[i + 1][j])) {
        cout << -1;
        return 0;
      }
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) sum += a[i][j];
  printf("%d", sum);
}
