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