#include <bits/stdc++.h>
using namespace std;
const int maxn = 600;
int aa[maxn][maxn];
int m, n, sum = 0, ans = 0;
void solve() {
  for (int i = n; i >= 1; i--) {
    for (int j = m; j >= 1; j--) {
      if (aa[i][j] == 0) {
        aa[i][j] = min(aa[i + 1][j], aa[i][j + 1]) - 1;
      }
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    for (int j = 1; j <= m - 1; j++) {
      if (aa[i][j] >= aa[i][j + 1] || aa[i][j] >= aa[i + 1][j]) {
        cout << "-1" << endl;
        return;
      } else {
        ans += aa[i][j];
      }
    }
  }
  for (int i = 1; i <= m - 1; i++) {
    if (aa[n][i] >= aa[n][i + 1]) {
      cout << "-1" << endl;
      return;
    } else {
      ans += aa[n][i];
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    if (aa[i][m] >= aa[i + 1][m]) {
      cout << "-1" << endl;
      return;
    } else {
      ans += aa[i][m];
    }
  }
  cout << ans + aa[n][m] << endl;
  return;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> aa[i][j];
    }
  }
  solve();
  return 0;
}
