Problem p03014 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define mx 100000

int main() {

  int n, m;
  cin >> n >> m;
  char ar[n + 2][m + 2];

  for (int i = 1; i <= n; i++) {
    scanf("%s", ar[i] + 1);
  }

  int row[n + 2][m + 2];
  int col[n + 2][m + 2];

  memset(row, 0, sizeof row);
  memset(col, 0, sizeof col);

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {

      if (ar[i][j] == '.') {

        col[i][j] = col[i][j - 1] + 1;
        row[i][j] = row[i - 1][j] + 1;
      }
    }
  }

  for (int i = 1; i <= n; i++) {

    // for(int j=1;j<=m;j++)cout<<row[i][j];
    // cout<<endl;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {

    for (int j = 1; j <= m; j++) {

      if (ar[i][j] == '#')
        continue;

      int lo = 1, hi = j;
      int res = 0;
      while (lo <= hi) {

        int mid = (lo + hi) / 2;
        if (row[i][j] - row[mid - 1][j] == i - mid + 1) {
          hi = mid - 1;
          res = max(res, i - mid);
        } else
          lo = mid + 1;
      }
      lo = i, hi = n;
      int res1 = 0;
      while (lo <= hi) {

        int mid = (lo + hi) / 2;
        if (row[mid][j] - row[i - 1][j] == mid - i + 1) {
          lo = mid + 1;
          res1 = max(res1, mid - i);
        } else
          hi = mid - 1;
      }
      lo = 1, hi = j;
      int res2 = 0;
      while (lo <= hi) {

        int mid = (lo + hi) / 2;
        if (col[i][j] - col[i][mid - 1] == j - mid + 1) {
          hi = mid - 1;
          res2 = max(res2, j - mid);
        } else
          lo = mid + 1;
      }
      lo = j, hi = m;
      int res3 = 0;
      while (lo <= hi) {

        int mid = (lo + hi) / 2;
        if (col[i][mid] - col[i][j - 1] == mid - j + 1) {
          lo = mid + 1;
          res3 = max(res3, mid - j);
        } else
          hi = mid - 1;
      }
      // if(i==2&&j==2)cout<<res<<res1<<res2<<res3<<endl;
      ans = max(ans, 1 + res + res1 + res2 + res3);
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define mx 100000

int main() {

  int n, m;
  cin >> n >> m;
  char ar[n + 2][m + 2];

  for (int i = 1; i <= n; i++) {
    scanf("%s", ar[i] + 1);
  }

  int row[n + 2][m + 2];
  int col[n + 2][m + 2];

  memset(row, 0, sizeof row);
  memset(col, 0, sizeof col);

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {

      if (ar[i][j] == '.') {

        col[i][j] = col[i][j - 1] + 1;
        row[i][j] = row[i - 1][j] + 1;
      }
    }
  }

  for (int i = 1; i <= n; i++) {

    // for(int j=1;j<=m;j++)cout<<row[i][j];
    // cout<<endl;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {

    for (int j = 1; j <= m; j++) {

      if (ar[i][j] == '#')
        continue;

      int lo = 1, hi = i;
      int res = 0;
      while (lo <= hi) {

        int mid = (lo + hi) / 2;
        if (row[i][j] - row[mid - 1][j] == i - mid + 1) {
          hi = mid - 1;
          res = max(res, i - mid);
        } else
          lo = mid + 1;
      }
      lo = i, hi = n;
      int res1 = 0;
      while (lo <= hi) {

        int mid = (lo + hi) / 2;
        if (row[mid][j] - row[i - 1][j] == mid - i + 1) {
          lo = mid + 1;
          res1 = max(res1, mid - i);
        } else
          hi = mid - 1;
      }
      lo = 1, hi = j;
      int res2 = 0;
      while (lo <= hi) {

        int mid = (lo + hi) / 2;
        if (col[i][j] - col[i][mid - 1] == j - mid + 1) {
          hi = mid - 1;
          res2 = max(res2, j - mid);
        } else
          lo = mid + 1;
      }
      lo = j, hi = m;
      int res3 = 0;
      while (lo <= hi) {

        int mid = (lo + hi) / 2;
        if (col[i][mid] - col[i][j - 1] == mid - j + 1) {
          lo = mid + 1;
          res3 = max(res3, mid - j);
        } else
          hi = mid - 1;
      }
      // if(i==2&&j==2)cout<<res<<res1<<res2<<res3<<endl;
      ans = max(ans, 1 + res + res1 + res2 + res3);
    }
  }

  cout << ans << endl;
  return 0;
}