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