#include <bits/stdc++.h>
int main() {
  int h, w;
  std::cin >> h >> w;
  int arr[h][w];
  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < w; ++x) {
      std::cin >> arr[y][x];
    }
  }
  long long result = 0;
  for (int y = h - 1; y >= 0; --y) {
    for (int x = w - 1; x >= 0; --x) {
      if (arr[y][x] == 0) {
        int r, d;
        if (x == w - 1)
          r = 8000;
        else
          r = arr[y][x + 1];
        if (y == h - 1)
          d = 8000;
        else
          d = arr[y + 1][x];
        arr[y][x] = std::min(r, d) - 1;
      }
      if (x != 0 && arr[y][x - 1] >= arr[y][x]) {
        result = -1;
        break;
      }
      if (y != 0 && arr[y - 1][x] >= arr[y][x]) {
        result = -1;
        break;
      }
      result += arr[y][x];
    }
    if (result == -1) break;
  }
  std::cout << result << '\n';
  return 0;
}
