#include <bits/stdc++.h>
const int INF = 1000000009;
const int MAXN = 100001;
const int MOD = 1000000007;
const double PI = 3.14159265359;
const double EPS = 1e-6;
using namespace std;
int maxv = -INF, gcdv;
int n, m, k, cnt, sum, ans;
int a[502][502];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 0) k = 1;
    }
  }
  for (int i = n - 1; i >= 1; i--) {
    for (int j = m - 1; j >= 1; j--) {
      if (a[i][j] == 0) a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1);
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 1; j < m; j++)
      if (a[i][j] <= a[i][j - 1]) {
        cout << -1 << '\n';
        return 0;
      }
  for (int i = 1; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] <= a[i - 1][j]) {
        cout << -1 << '\n';
        return 0;
      }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) sum += a[i][j];
  cout << sum << '\n';
  return 0;
};
