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

#include "stdio.h"

int ido(int x, int y, int aw[90][90]) {
  int a[90][90];
  for (int i = 0; i < 90; i++) {
    for (int ii = 0; ii < 90; ii++) {
      a[i][ii] = aw[i][ii];
    }
  }
  a[x][y] = 0;
  int max = 0;
  if (x + 1 < 90) {
    if (a[x + 1][y] == 1) {
      int w = ido(x + 1, y, a);
      if (max < w) {
        max = w;
      }
    }
  }
  if (0 <= x - 1) {
    if (a[x - 1][y] == 1) {
      int w = ido(x - 1, y, a);
      if (max < w) {
        max = w;
      }
    }
  }
  if (y + 1 < 90) {
    if (a[x][y + 1] == 1) {
      int w = ido(x, y + 1, a);
      if (max < w) {
        max = w;
      }
    }
  }
  if (0 <= y - 1) {
    if (a[x][y - 1] == 1) {
      int w = ido(x, y - 1, a);
      if (max < w) {
        max = w;
      }
    }
  }
  return max + 1;
}

int main() {
  while (1) {
    int m, n;
    scanf("%d", &m);
    scanf("%d", &n);
    if (n == 0 && m == 0) {
      break;
    }
    int aw[90][90];
    for (int i = 0; i < 90; i++) {
      for (int ii = 0; ii < 90; ii++) {
        aw[i][ii] = 0;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int ii = 0; ii < m; ii++) {
        scanf("%d", &aw[i][ii]);
      }
    }
    int max = 0;
    for (int i = 0; i < n; i++) {
      for (int ii = 0; ii < m; ii++) {
        if (aw[i][ii] == 1) {
          int h = 0;
          if (0 <= i - 1) {
            h += aw[i - 1][ii];
          }
          if (i + 1 < 90) {
            h += aw[i + 1][ii];
          }
          if (0 <= ii - 1) {
            h += aw[i][ii - 1];
          }
          if (ii + 1 < 90) {
            h += aw[i][ii + 1];
          }
          if (h == 1) {
            int w = ido(i, ii, aw);
            if (max < w) {
              max = w;
            }
          }
        }
      }
    }
    printf("%d\\n", max);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "stdio.h"

int ido(int x, int y, int aw[90][90]) {
  int a[90][90];
  for (int i = 0; i < 90; i++) {
    for (int ii = 0; ii < 90; ii++) {
      a[i][ii] = aw[i][ii];
    }
  }
  a[x][y] = 0;
  int max = 0;
  if (x + 1 < 90) {
    if (a[x + 1][y] == 1) {
      int w = ido(x + 1, y, a);
      if (max < w) {
        max = w;
      }
    }
  }
  if (0 <= x - 1) {
    if (a[x - 1][y] == 1) {
      int w = ido(x - 1, y, a);
      if (max < w) {
        max = w;
      }
    }
  }
  if (y + 1 < 90) {
    if (a[x][y + 1] == 1) {
      int w = ido(x, y + 1, a);
      if (max < w) {
        max = w;
      }
    }
  }
  if (0 <= y - 1) {
    if (a[x][y - 1] == 1) {
      int w = ido(x, y - 1, a);
      if (max < w) {
        max = w;
      }
    }
  }
  return max + 1;
}

int main() {
  while (1) {
    int m, n;
    scanf("%d", &m);
    scanf("%d", &n);
    if (n == 0 && m == 0) {
      break;
    }
    int aw[90][90];
    for (int i = 0; i < 90; i++) {
      for (int ii = 0; ii < 90; ii++) {
        aw[i][ii] = 0;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int ii = 0; ii < m; ii++) {
        scanf("%d", &aw[i][ii]);
      }
    }
    int max = 0;
    for (int i = 0; i < n; i++) {
      for (int ii = 0; ii < m; ii++) {
        if (aw[i][ii] == 1) {
          int w = ido(i, ii, aw);
          if (max < w) {
            max = w;
          }
        }
      }
    }
    printf("%d\\n", max);
  }
  return 0;
}