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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
int n;
char f[1005][1005];
int imos[1005][1005];
int culc(int i, int j, int x) {
  if (i - x < 0 || j - x < 0)
    return 0;
  return imos[i][j] - imos[i][j - x] - imos[i - x][j] + imos[i - x][j - x];
}
int main() {
  int cas = 0;
  while (1) {
    cas++;
    int ans = 0;
    cin >> n;
    if (n == 0)
      break;
    memset(imos, 0, sizeof(imos));
    memset(f, 0, sizeof(f));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        cin >> f[i][j];
        if (f[i][j] == '.')
          imos[i][j]++;
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 2; j <= n; j++) {
        imos[j][i] += imos[j - 1][i];
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 2; j <= n; j++) {
        imos[i][j] += imos[i][j - 1];
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        int l = ans, r = n + 1;
        while (r - l > 1) {
          int mid = (l + r) / 2;
          if (culc(i, j, mid) == mid * mid)
            l = mid;
          else
            r = mid;
        }
        ans = max(ans, l);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
char f[1001][1001];
int n;
int ans;
bool C(int x, int y, int p) {
  for (int i = x; i < x + p; i++) {
    for (int j = y; j < y + p; j++) {
      if (i >= n || j >= n)
        return false;
      if (f[i][j] == '*')
        return false;
    }
  }
  return true;
}
int check(int x, int y) {
  if (!C(x, y, ans + 1))
    return 0;
  int l = ans + 1, r = n + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (C(x, y, mid))
      l = mid;
    else
      r = mid;
  }
  return l;
}
int main() {
  while (1) {
    ans = 0;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> f[i][j];
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (f[i][j] == '.') {
          ans = max(ans, check(i, j));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}