#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(n, vector<int>(m));
  for (auto &it : a)
    for (auto &itt : it) cin >> itt;
  for (int i = n - 2; i > 0; --i) {
    for (int j = m - 2; j > 0; --j) {
      if (a[i][j] == 0) {
        a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j < m - 1; ++j) {
      if (a[i][j] > a[i][j - 1] && a[i][j + 1] > a[i][j]) continue;
      cout << -1;
      return 0;
    }
  }
  for (int i = 1; i < n - 1; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i][j] > a[i - 1][j] && a[i][j] < a[i + 1][j]) continue;
      cout << -1;
      return 0;
    }
  }
  long long ans = 0;
  for (auto it : a)
    for (auto itt : it) ans += itt;
  cout << ans;
  return 0;
}
