#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1& arg1) {
  cerr << name << ": " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1& arg1, Args&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " /";
  __f(comma + 1, args...);
}
template <class T>
bool mini(T& a, T b) {
  return a > b ? (a = b, 0) : 1;
}
template <class T>
bool maxi(T& a, T b) {
  return a < b ? (a = b, 0) : 1;
}
const int INF = 1e9 + 7;
const int N = 503;
int n, m, s;
int a[N][N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = (1), _b = (n); i <= (_b); ++i)
    for (int j = (1), _b = (m); j <= (_b); ++j) cin >> a[i][j];
  for (int i = (n - 1), _b = (1); i >= (_b); --i)
    for (int j = (m - 1), _b = (1); j >= (_b); --j) {
      if (a[i][j] == 0) a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
    }
  for (int i = (1), _b = (n); i <= (_b); ++i)
    for (int j = (1), _b = (m); j <= (_b); ++j) s += a[i][j];
  for (int i = (1), _b = (n); i <= (_b); ++i)
    for (int j = (1), _b = (m); j <= (_b); ++j)
      if (a[i][j] <= a[i - 1][j] || a[i][j] <= a[i][j - 1]) {
        cout << "-1\n";
        return 0;
      }
  cout << s << '\n';
  return 0;
}
