#include <bits/stdc++.h>
using namespace std;
int n, m;
int arr[600][600];
long long judge() {
  long long sum = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) sum += arr[i][j];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j < m; ++j)
      if (arr[i][j + 1] <= arr[i][j]) return -1;
  for (int j = 1; j <= m; ++j)
    for (int i = 1; i < n; ++i)
      if (arr[i + 1][j] <= arr[i][j]) return -1;
  return sum;
}
int main() {
  memset(arr, -1, sizeof(arr));
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) cin >> arr[i][j];
  for (int i = n; i >= 1; --i)
    for (int j = m; j >= 1; --j) {
      if (arr[i][j] == 0) {
        int p = min(arr[i][j + 1], arr[i + 1][j]);
        if (p - 1 <= 0) {
          cout << -1 << endl;
          return 0;
        }
        arr[i][j] = p - 1;
      }
    }
  cout << judge() << endl;
}
