#include <bits/stdc++.h>
using namespace std;
int msb(long long x) {
  union {
    double a;
    int32_t b[2];
  };
  a = x;
  return (b[1] >> 20) - 1023;
}
vector<int> a[509];
void sol() {
  int n, m, t, ans = 0;
  cin >> n >> m;
  for (int i = (0); i < (n); i++) a[i].clear();
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++) {
      cin >> t;
      a[i].push_back(t);
    }
  for (int i = (n)-1; i >= (0); i--)
    for (int j = (m)-1; j >= (0); j--) {
      if (a[i][j] == 0) {
        if (i == n - 1 && j == m - 1)
          a[i][j] = 8000;
        else if (i == n - 1)
          a[i][j] = a[i][j + 1] - 1;
        else if (j == m - 1)
          a[i][j] = a[i + 1][j] - 1;
        else
          a[i][j] = min(a[i][j + 1], a[i + 1][j]) - 1;
      } else if (j != m - 1 && a[i][j] >= a[i][j + 1]) {
        cout << "-1\n";
        return;
      } else if (i != n - 1 && a[i][j] >= a[i + 1][j]) {
        cout << "-1\n";
        return;
      }
      ans += a[i][j];
    }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) sol();
}
