#include <bits/stdc++.h>
using LL = long long;
using ULL = unsigned long long;
using db = double;
const int R = 27;
const int MOD = 19260817;
const int maxn = 500 + 10;
using namespace std;
int a[maxn][maxn];
int main(int argc, char **argv) {
  int n, m, ans = 0, flag = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] && ((a[i][j] <= a[i - 1][j]) || (a[i][j] <= a[i][j - 1]))) {
        flag = 1;
      }
    }
  }
  if (flag) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = n - 1; i > 1; i--) {
    for (int j = m - 1; j > 1; j--) {
      if (!a[i][j]) {
        a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
        if (a[i][j] <= a[i - 1][j] || a[i][j] <= a[i][j - 1]) {
          cout << -1 << endl;
          return 0;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) ans += a[i][j];
  cout << ans << endl;
  return 0;
}
