#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, i, j, n, m;
  cin >> n >> m;
  int a[n + 5][m + 5];
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (i = n, j = 0; j < m; j++) {
    a[i][j] = 900000;
  }
  for (j = m, i = 0; i < n; i++) {
    a[i][j] = 900000;
  }
  int sum = 0, ck = 0;
  for (i = n - 1; i >= 0; i--) {
    for (j = m - 1; j >= 0; j--) {
      if (a[i][j] == 0 || a[i][j] == 900000) {
        int x = j + 1;
        int y = i + 1;
        if (a[i][x] == 0) a[i][x] = 900000;
        if (a[y][j] == 0) a[y][j] = 900000;
        a[i][j] = min(a[i][x], a[y][j]);
        a[i][j]--;
      }
      sum += a[i][j];
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 1; j < m; j++) {
      if (a[i][j - 1] >= a[i][j]) ck++;
    }
  }
  for (j = 0; j < m; j++) {
    for (i = 1; i < n; i++) {
      if (a[i - 1][j] >= a[i][j]) ck++;
    }
  }
  if (ck > 0)
    cout << "-1" << endl;
  else
    cout << sum << endl;
  return 0;
}
