Problem p03530 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define mp make_pair
#define reg register int
#define pii pair<int, int>
#define fo(i, a, b) for (reg i = a; i <= b; i++)
#define fd(i, a, b) for (reg i = a; i >= b; i--)
#define cr const reg &
using namespace std;
const int N = 42;

I int _max(cr x, cr y) { return x > y ? x : y; }
I int _min(cr x, cr y) { return x < y ? x : y; }
I int read() {
  reg x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
I void ptt(LL x) {
  if (x >= 10)
    ptt(x / 10);
  putchar(x % 10 + '0');
}
I void put(LL x) { x < 0 ? putchar('-'), ptt(-x) : ptt(x); }
I void pr1(LL x) { put(x), putchar(' '); }
I void pr2(LL x) { put(x), puts(""); }

char ss[N][N];
int s[N][N], f[N][N][N][N];

I int S(cr a, cr b, cr c, cr d) {
  return s[c][d] - s[a - 1][d] - s[c][b - 1] + s[a - 1][b - 1];
}
I void upd(int &x, int c) { x = _min(x, c); }

int main() {
  reg n = read(), m = read(), px, py;
  fo(i, 1, n) {
    scanf("%s", ss[i] + 1);
    fo(j, 1, m) {
      if (ss[i][j] == 'P')
        px = i, py = j;
      else if (ss[i][j] == '#')
        s[i][j] = 1;
      s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
    }
  }
  memset(f, 63, sizeof(f));
  reg ans = f[0][0][0][0];
  f[1][1][n][m] = 0;
  fo(i, 1, n) fo(j, 1, m) fd(k, n, i)
      fd(h, m, j) if (i <= px && j <= py && k >= px && h >= py) {
    reg c = f[i][j][k][h];
    fo(u, i, k) fo(v, j, h) {
      reg s1 = S(i, j, u, v) + S(u + 1, v + 1, k, h);
      reg s2 = S(i, v + 1, u, h) + S(u + 1, j, k, v);
      upd(f[i][j][u][v], c + s2), upd(f[u + 1][v + 1][k][h], c + s2);
      upd(f[i][v + 1][u][h], c + s1), upd(f[u + 1][j][k][v], c + s1);
    }
    ans = _min(ans, c + _min(_min(S(i, j, px, py), S(i, py, px, h)),
                             _min(S(px, j, k, py), S(px, py, k, h))));
  }
  pr2(ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define mp make_pair
#define reg register int
#define pii pair<int, int>
#define fo(i, a, b) for (reg i = a; i <= b; i++)
#define fd(i, a, b) for (reg i = a; i >= b; i--)
#define cr const reg &
using namespace std;
const int N = 41;

I int _max(cr x, cr y) { return x > y ? x : y; }
I int _min(cr x, cr y) { return x < y ? x : y; }
I int read() {
  reg x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
I void ptt(LL x) {
  if (x >= 10)
    ptt(x / 10);
  putchar(x % 10 + '0');
}
I void put(LL x) { x < 0 ? putchar('-'), ptt(-x) : ptt(x); }
I void pr1(LL x) { put(x), putchar(' '); }
I void pr2(LL x) { put(x), puts(""); }

char ss[N][N];
int s[N][N], f[N][N][N][N];

I int S(cr a, cr b, cr c, cr d) {
  return s[c][d] - s[a - 1][d] - s[c][b - 1] + s[a - 1][b - 1];
}
I void upd(int &x, int c) { x = _min(x, c); }

int main() {
  reg n = read(), m = read(), px, py;
  fo(i, 1, n) {
    scanf("%s", ss[i] + 1);
    fo(j, 1, m) {
      if (ss[i][j] == 'P')
        px = i, py = j;
      else if (ss[i][j] == '#')
        s[i][j] = 1;
      s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
    }
  }
  memset(f, 63, sizeof(f));
  reg ans = f[0][0][0][0];
  f[1][1][n][m] = 0;
  fo(i, 1, n) fo(j, 1, m) fd(k, n, i)
      fd(h, m, j) if (i <= px && j <= py && k >= px && h >= py) {
    reg c = f[i][j][k][h];
    fo(u, i, k) fo(v, j, h) {
      reg s1 = S(i, j, u, v) + S(u + 1, v + 1, k, h);
      reg s2 = S(i, v + 1, u, h) + S(u + 1, j, k, v);
      upd(f[i][j][u][v], c + s2), upd(f[u + 1][v + 1][k][h], c + s2);
      upd(f[i][v + 1][u][j], c + s1), upd(f[u + 1][j][k][v], c + s1);
    }
    ans = _min(ans, c + _min(_min(S(i, j, px, py), S(i, py, px, h)),
                             _min(S(px, j, k, py), S(px, py, k, h))));
  }
  pr2(ans);
  return 0;
}