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

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI = 6 * asin(0.5);
typedef pair<double, double> P;
typedef complex<double> CP;
typedef long long ll;

int a[1010][1010] = {0};
ll b[1010][1010] = {0};

int main() {
  int n;
  string s;
  while (cin >> n, n) {
    /*
    rep(i,n){
            rep(j,n){
                    b[i][j]=0;
            }
    }
    */
    rep(i, n) {
      cin >> s;
      rep(j, n) {
        if (s[j] == '.') {
          a[i + 1][j + 1] = 0;
        } else
          a[i + 1][j + 1] = 1;
      }
    }

    rep(i, n) {
      rep(j, n) {
        if (j == 0 && i == 0) {
          b[1][1] = a[1][1];
        } else if (j == 0) {
          b[i + 1][1] = b[i][1] + a[i + 1][1];
        } else if (i == 0) {
          b[1][j + 1] = b[1][j] + a[1][j + 1];
        } else {
          b[i + 1][j + 1] =
              b[i + 1][j] + b[i][j + 1] - b[i][j] + a[i + 1][j + 1];
        }
        // printf("%3d",b[i+1][j+1]);
      }
      // cout<<endl;
    }
    // cout<<endl;
    // cout<<b[n][n]<<endl;
    ll res = 0;
    for (int i = 0; i < n; i++) {
      if (n - i < res)
        break;
      for (int j = 0; j < n; j++) {
        if (n - j < res)
          break;
        ll M = 0;
        while (1) {
          if (!(i + M + 1 <= n && j + M + 1 <= n)) {
            break;
          }
          if (b[i + M + 1][j + M + 1] - b[i + M + 1][j] - b[i][j + M + 1] +
                  b[i][j] ==
              0) {
            M++;
          } else {
            break;
          }
        }
        res = max(res, M);
        // cout<<i<<" "<<j<<" "<<M-1<<endl;
      }
    }
    cout << res << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI = 6 * asin(0.5);
typedef pair<double, double> P;
typedef complex<double> CP;
typedef long long ll;

int a[1010][1010] = {0};
ll b[1010][1010] = {0};

int main() {
  int n;
  string s;
  while (cin >> n, n) {
    /*
    rep(i,n){
            rep(j,n){
                    b[i][j]=0;
            }
    }
    */
    rep(i, n) {
      cin >> s;
      rep(j, n) {
        if (s[j] == '.') {
          a[i + 1][j + 1] = 0;
        } else
          a[i + 1][j + 1] = 1;
      }
    }

    rep(i, n) {
      rep(j, n) {
        if (j == 0 && i == 0) {
          b[1][1] = a[1][1];
        } else if (j == 0) {
          b[i + 1][1] = b[i][1] + a[i + 1][1];
        } else if (i == 0) {
          b[1][j + 1] = b[1][j] + a[1][j + 1];
        } else {
          b[i + 1][j + 1] =
              b[i + 1][j] + b[i][j + 1] - b[i][j] + a[i + 1][j + 1];
        }
        // printf("%3d",b[i+1][j+1]);
      }
      // cout<<endl;
    }
    // cout<<endl;
    // cout<<b[n][n]<<endl;
    ll res = 0;
    rep(i, n) {
      rep(j, n) {
        ll M = 0;
        while (1) {
          if (!(i + M <= n && j + M <= n)) {
            break;
          }
          if (b[i + M + 1][j + M + 1] - b[i + M + 1][j] - b[i][j + M + 1] +
                  b[i][j] ==
              0) {
            M++;
          } else {
            break;
          }
        }
        res = max(res, M);
        // cout<<i<<" "<<j<<" "<<M-1<<endl;
      }
    }
    res = min((ll)n, res);
    cout << res << endl;
  }
  return 0;
}