Problem p00116 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

int main(void) {

  int n = 0, s[21] = {0};

  bool r[501][501];
  int h, w, i, j, k, l;
  char c[501][501];
  do {
    scanf("%d%d", &h, &w);

    for (i = 0; i < h; i++) {
      scanf("%s", c[i]);
      for (j = 0; j < w; j++) {
        if (c[i][j] == '*') {
          r[i][j] = false;
        } else {
          r[i][j] = true;
        }
      }
    }
    for (i = 0; i < h; i++) {
      r[i][w] = false;
    }
    for (j = 0; j < w; j++) {
      r[h][j] = false;
    }

    for (i = 0; i < h; i++) {
      // printf("iok\\n");
      for (j = 0; j < w; j++) {
        //	printf("jok\\n");
        if (r[i][j] == true) {
          //		printf("kansoku\\n");
          int a, b = 0;
          while (r[i + b][j] == true) {
            a = 1;
            while (true) {
              l = 0;

              for (k = i; k <= i + b; k++) {
                if (r[k][j + a] == false)
                  l++;
                if (r[k][j + a] == false)
                  break;
              }
              // printf("i=%d,j=%d,b=%d,a=%d,l=%d\\n",i,j,b,a,l);
              if (l != 0 || a + j == w) {
                if (s[n] < (a) * (b + 1)) {
                  //	printf("i=%d,j=%d,a=%d,b+1=%d\\n",i,j,a,b+1);
                  s[n] = (a) * (b + 1);
                }
              } else {
                a++;
              }
              if (l != 0)
                break;
            }
            b++;
          }
        }
      }
    }

    n++;

  } while (w != 0 || h != 0);

  for (i = 0; i < n - 1; i++) {
    printf("%d\\n", s[i]);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>

int main(void) {

  int n = 0, s[21] = {0};

  bool r[501][501];
  int h, w, i, j, k, l;
  char c[501][501];
  do {
    scanf("%d%d", &h, &w);

    for (i = 0; i < h; i++) {
      scanf("%s", c[i]);
      for (j = 0; j < w; j++) {
        if (c[i][j] == '*') {
          r[i][j] = false;
        } else {
          r[i][j] = true;
        }
      }
    }
    for (i = 0; i < h; i++) {
      r[i][w] = false;
    }
    for (j = 0; j < w; j++) {
      r[h][j] = false;
    }

    for (i = 0; i < h; i++) {
      // printf("iok\\n");
      for (j = 0; j < w; j++) {
        //	printf("jok\\n");
        if (r[i][j] == true) {
          //		printf("kansoku\\n");
          int a, b = 0;
          while (r[i + b][j] == true) {
            a = 0;
            while (true) {
              l = 0;

              for (k = i; k <= i + b; k++) {
                if (r[k][j + a] == false)
                  l++;
                if (r[k][j + a] == false)
                  break;
              }
              // printf("i=%d,j=%d,b=%d,a=%d,l=%d\\n",i,j,b,a,l);
              if (l != 0 || a + j == w) {
                if (s[n] < (a) * (b + 1)) {
                  //	printf("i=%d,j=%d,a=%d,b+1=%d\\n",i,j,a,b+1);
                  s[n] = (a) * (b + 1);
                }
              } else {
                a++;
              }
              if (l != 0)
                break;
            }
            b++;
          }
        }
      }
    }

    n++;

  } while (w != 0 || h != 0);

  for (i = 0; i < n; i++) {
    printf("%d\\n", s[i]);
  }
  return 0;
}