Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#ifndef LL
#define LL long long
#endif
#ifndef uLL
#define uLL unsigned long long
#endif
#ifndef uL
#define uL unsigned int
#endif
#ifndef iL
#define iL int
#endif
#ifndef iS
#define iS short
#endif
#ifndef uS
#define uS unsigned short
#endif
#ifndef uC
#define uC unsigned char
#endif
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define abs(a) (((a) < 0) ? (-(a)) : (a))
#define sgn(a) (((a) < 0) ? (1) : (0))
#define movs(a, b, l) (memmove((a), (b), sizeof(*(b)) * (l)))
#define stosb(a, b, l) (memset((a), (b), sizeof(*(a)) * (l)))
LL map[1000][1000];
LL di[][2] = {{1, 0}, {0, 1}, {(LL)0 - 1, 0}, {0, (LL)0 - 1}};
int legal(char ch) {
  if (ch == '#') {
    return 1;
  }
  if (ch == '.') {
    return 0;
  }
  return 2;
}
LL que[2000000];
int main() {
  // freopen("in.in","r",stdin);
  char ch;
  LL i, j, k, a, b, c, d, n, m, ans;
  *que = 0;
  scanf("%lld%lld", &n, &m);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
    RE:
      scanf("%c", &ch);
      if (legal(ch) == 2) {
        goto RE;
      }
      map[i][j] = legal(ch);
      if (map[i][j] == 1) {
        que[++(*que)] = (i << 20) | j;
      }
    }
  }
  for (i = 1; i <= *que; i++) {
    a = que[i] >> 20;
    b = que[i] & 0xFFFFF;
    // printf("%lld %lld\n",a,b);
    for (j = 0; j < 4; j++) {
      c = a + di[j][0];
      d = b + di[j][1];
      // printf("%lld %lld*\n",c,d);
      if ((c >= 1) && (c <= n) && (d >= 1) && (d <= m) && (map[c][d] == 0)) {
        map[c][d] = map[a][b] + 1;
        que[++(*que)] = (c << 20) | d;
      }
    }
  }
  ans = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      ans = max(ans, map[i][j]);
    }
  }
  printf("%lld\n", ans - 1);
  return 0;
}

Fixed submission:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#ifndef LL
#define LL long long
#endif
#ifndef uLL
#define uLL unsigned long long
#endif
#ifndef uL
#define uL unsigned int
#endif
#ifndef iL
#define iL int
#endif
#ifndef iS
#define iS short
#endif
#ifndef uS
#define uS unsigned short
#endif
#ifndef uC
#define uC unsigned char
#endif
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define abs(a) (((a) < 0) ? (-(a)) : (a))
#define sgn(a) (((a) < 0) ? (1) : (0))
#define movs(a, b, l) (memmove((a), (b), sizeof(*(b)) * (l)))
#define stosb(a, b, l) (memset((a), (b), sizeof(*(a)) * (l)))
LL map[1007][1007];
LL di[][2] = {{1, 0}, {0, 1}, {(LL)0 - 1, 0}, {0, (LL)0 - 1}};
int legal(char ch) {
  if (ch == '#') {
    return 1;
  }
  if (ch == '.') {
    return 0;
  }
  return 2;
}
LL que[2000000];
int main() {
  // freopen("in.in","r",stdin);
  char ch;
  LL i, j, k, a, b, c, d, n, m, ans;
  *que = 0;
  scanf("%lld%lld", &n, &m);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
    RE:
      scanf("%c", &ch);
      if (legal(ch) == 2) {
        goto RE;
      }
      map[i][j] = legal(ch);
      if (map[i][j] == 1) {
        que[++(*que)] = (i << 20) | j;
      }
    }
  }
  for (i = 1; i <= *que; i++) {
    a = que[i] >> 20;
    b = que[i] & 0xFFFFF;
    // printf("%lld %lld\n",a,b);
    for (j = 0; j < 4; j++) {
      c = a + di[j][0];
      d = b + di[j][1];
      // printf("%lld %lld*\n",c,d);
      if ((c >= 1) && (c <= n) && (d >= 1) && (d <= m) && (map[c][d] == 0)) {
        map[c][d] = map[a][b] + 1;
        que[++(*que)] = (c << 20) | d;
      }
    }
  }
  ans = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      ans = max(ans, map[i][j]);
    }
  }
  printf("%lld\n", ans - 1);
  return 0;
}