#include <bits/stdc++.h>
using namespace std;
int a[505][505];
int main() {
  int n, m, flag = 1, 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]);
      sum += a[i][j];
    }
  }
  for (int i = n - 1; i >= 0 && flag; i--) {
    for (int j = m - 1; j >= 0; j--) {
      if (!a[i][j]) {
        a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
        sum += a[i][j];
      } else {
        if ((i < n - 1 && a[i][j] >= a[i + 1][j]) ||
            (j < m - 1 && a[i][j] >= a[i][j + 1])) {
          flag = 0;
          break;
        }
      }
    }
  }
  if (flag)
    printf("%d\n", sum);
  else
    printf("-1\n");
  return 0;
}
