#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  t = 1;
  while (t--) {
    int n, m, i, j;
    cin >> n >> m;
    int a[n + 2][m + 2];
    int ii = -1, jj = -1;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        cin >> a[i][j];
        if (a[i][j] > 0) ii = i;
        jj = j;
      }
    }
    if (ii == -1)
      cout << jj << endl;
    else {
      for (i = ii; i >= 0; i--) {
        for (j = jj; j >= 0; j--) {
          int mn = 9000;
          if (a[i][j] == 0) {
            if ((i + 1) < n) mn = min(mn, a[i + 1][j]);
            if ((j + 1) < m) mn = min(mn, a[i][j + 1]);
            a[i][j] = mn - 1;
          }
        }
      }
      int sum = 0, in = 1;
      for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
          sum += a[i][j];
          if (i < n - 1 && j < m)
            if (a[i + 1][j] <= a[i][j]) in = 0;
          if (j < m - 1 && i < n)
            if (a[i][j + 1] <= a[i][j]) in = 0;
        }
      }
      if (in == 1)
        cout << sum << endl;
      else
        cout << -1 << endl;
    }
  }
  return 0;
}
