#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, flag = 1, sum = 0;
  int a[510][510];
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i][j];
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] != 0) {
        if (i + 1 < n && j + 1 < m &&
            (a[i][j] >= a[i + 1][j] || a[i][j] >= a[i][j + 1])) {
          flag = 0;
          break;
        } else if (i == n - 1 && j + 1 < m && a[i][j] >= a[i][j + 1]) {
          flag = 0;
          break;
        } else if (j == m - 1 && i + 1 < n && a[i][j] >= a[i + 1][j]) {
          flag = 0;
          break;
        }
      } else if (a[i][j] == 0) {
        if (j + 1 < m && i + 1 < n)
          a[i][j] = min(a[i][j + 1], a[i + 1][j]) - 1;
        else if (j == m - 1 && i + 1 < n)
          a[i][j] = a[i + 1][j] - 1;
        else if (i == n - 1 && j + 1 < m)
          a[i][j] = a[i][j + 1] - 1;
        else if (i == n - 1 && j == m - 1)
          a[i][j] = 8000;
      }
      sum = sum + a[i][j];
    }
    if (flag == 0) break;
  }
  if (flag == 0)
    cout << -1;
  else
    cout << sum;
  return 0;
}
