Problem p01416 (C++)

Buggy submission (status: Runtime Error):
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void gstr(char *str) {
  int i;
  for (i = 0; (str[i] = getchar()) != '\n'; i++)
    ;
  str[i] = '\0';
}

int main(void) {
  int i, j, k, l, m, n, h, w, dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0},
                              ans = 0, f[20][20];
  char s[500][501], *c[4];
  scanf("%d%d%*c", &h, &w);
  for (i = 0; i < h; i++)
    gstr(s[i]);
  memset(f, 0, sizeof(f));
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (isupper(s[i][j])) {
        for (k = 0; k < 4; k++) {
          for (l = 1;
               i + dy[k] * l >= 0 && i + dy[k] * l < h && j + dx[k] * l >= 0 &&
               j + dx[k] * l < w && !isupper(s[i + dy[k] * l][j + dx[k] * l]);
               l++)
            f[i + dy[k] * l][j + dx[k] * l]++;
        }
      }
    }
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (!isupper(s[i][j]) && f[i][j] >= 2) {
        for (k = 0; k < 4; k++) {
          for (l = 0;
               i + dy[k] * l >= 0 && i + dy[k] * l < h && j + dx[k] * l >= 0 &&
               j + dx[k] * l < w && !isupper(s[i + dy[k] * l][j + dx[k] * l]);
               l++)
            ;
          if (i + dy[k] * l >= 0 && i + dy[k] * l < h && j + dx[k] * l >= 0 &&
              j + dx[k] * l < w)
            c[k] = &s[i + dy[k] * l][j + dx[k] * l];
          else
            c[k] = NULL;
        }
        for (k = 0; k < 3; k++) {
          for (l = k + 1; l < 4; l++)
            if (c[k] != NULL && c[l] != NULL && k != l && *c[k] == *c[l] &&
                *c[k] != '.')
              *c[k] = '.', *c[l] = '.', i = -1, ans += 2;
        }
        if (i == -1) {
          memset(f, 0, sizeof(f));
          for (k = 0; k < h; k++) {
            for (l = 0; l < w; l++) {
              if (isupper(s[k][l])) {
                for (m = 0; m < 4; m++) {
                  for (n = 1; k + dy[m] * n >= 0 && k + dy[m] * n < h &&
                              l + dx[m] * n >= 0 && l + dx[m] * n < w &&
                              !isdigit(s[k + dy[m] * n][l + dx[m] * n]);
                       n++) {
                    f[k + dy[m] * n][l + dx[m] * n]++;
                  }
                }
              }
            }
          }
        }
      }
      if (i == -1)
        break;
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void gstr(char *str) {
  int i;
  for (i = 0; (str[i] = getchar()) != '\n'; i++)
    ;
  str[i] = '\0';
}

int main(void) {
  int i, j, k, l, m, n, h, w, dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0},
                              ans = 0, f[500][500];
  char s[500][501], *c[4];
  scanf("%d%d%*c", &h, &w);
  for (i = 0; i < h; i++)
    gstr(s[i]);
  memset(f, 0, sizeof(f));
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (isupper(s[i][j])) {
        for (k = 0; k < 4; k++) {
          for (l = 1;
               i + dy[k] * l >= 0 && i + dy[k] * l < h && j + dx[k] * l >= 0 &&
               j + dx[k] * l < w && !isupper(s[i + dy[k] * l][j + dx[k] * l]);
               l++)
            f[i + dy[k] * l][j + dx[k] * l]++;
        }
      }
    }
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (!isupper(s[i][j]) && f[i][j] >= 2) {
        for (k = 0; k < 4; k++) {
          for (l = 0;
               i + dy[k] * l >= 0 && i + dy[k] * l < h && j + dx[k] * l >= 0 &&
               j + dx[k] * l < w && !isupper(s[i + dy[k] * l][j + dx[k] * l]);
               l++)
            ;
          if (i + dy[k] * l >= 0 && i + dy[k] * l < h && j + dx[k] * l >= 0 &&
              j + dx[k] * l < w)
            c[k] = &s[i + dy[k] * l][j + dx[k] * l];
          else
            c[k] = NULL;
        }
        for (k = 0; k < 3; k++) {
          for (l = k + 1; l < 4; l++)
            if (c[k] != NULL && c[l] != NULL && k != l && *c[k] == *c[l] &&
                *c[k] != '.')
              *c[k] = '.', *c[l] = '.', i = -1, ans += 2;
        }
        if (i == -1) {
          memset(f, 0, sizeof(f));
          for (k = 0; k < h; k++) {
            for (l = 0; l < w; l++) {
              if (isupper(s[k][l])) {
                for (m = 0; m < 4; m++) {
                  for (n = 1; k + dy[m] * n >= 0 && k + dy[m] * n < h &&
                              l + dx[m] * n >= 0 && l + dx[m] * n < w &&
                              !isdigit(s[k + dy[m] * n][l + dx[m] * n]);
                       n++) {
                    f[k + dy[m] * n][l + dx[m] * n]++;
                  }
                }
              }
            }
          }
        }
      }
      if (i == -1)
        break;
    }
  }
  printf("%d\n", ans);
  return 0;
}