Problem p00092 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
char grid[12][12];
int dp[12][12];

void disp_grid(int n) {
  cout << "grid: " << endl;
  rep(i, n) {
    rep(j, n) { cout << grid[i][j]; } // end rep
    cout << endl;
  } // end rep
}
void disp_dp(int n) {
  cout << "dp: " << endl;
  rep(i, n) {
    rep(j, n) { cout << dp[i][j]; } // end rep
    cout << endl;
  } // end rep
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.square_searching", "r", stdin );
  int n;
  while (scanf("%d", &n), n) {
    memset(grid, 0, sizeof(grid));
    memset(dp, 0, sizeof(dp));
    rep(i, n) {
      rep(j, n) { scanf(" %c", &grid[i][j]); } // end rep
    }                                          // end rep

    dp[0][0] = (grid[0][0] == '.' ? 1 : 0);
    for (int i = 1; i < n; i++) {
      dp[i][0] = (grid[i][0] == '*' ? 0 : 1);
    } // end for
    for (int j = 1; j < n; j++) {
      dp[0][j] = (grid[0][j] == '*' ? 0 : 1);
    } // end for
    for (int i = 1; i < n; i++) {
      for (int j = 1; j < n; j++) {
        dp[i][j] =
            (grid[i][j] == '*'
                 ? 0
                 : min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1);
      } // end for
    }   // end for

    //		disp_grid (n );
    //		disp_dp (n );
    int res = 0;
    for (int i = 1; i < n; i++) {
      for (int j = 1; j < n; j++) {
        res = max(res, dp[i][j]);
      } // end for
    }   // end for
    printf("%d\n", res);
  } // end loop

  return 0;
}

Fixed submission:
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
char grid[1005][1005];
int dp[1005][1005];

void disp_grid(int n) {
  cout << "grid: " << endl;
  rep(i, n) {
    rep(j, n) { cout << grid[i][j]; } // end rep
    cout << endl;
  } // end rep
}
void disp_dp(int n) {
  cout << "dp: " << endl;
  rep(i, n) {
    rep(j, n) { cout << dp[i][j]; } // end rep
    cout << endl;
  } // end rep
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.square_searching", "r", stdin );
  int n;
  while (scanf("%d", &n), n) {
    memset(grid, 0, sizeof(grid));
    memset(dp, 0, sizeof(dp));
    rep(i, n) {
      rep(j, n) { scanf(" %c", &grid[i][j]); } // end rep
    }                                          // end rep

    dp[0][0] = (grid[0][0] == '.' ? 1 : 0);
    for (int i = 1; i < n; i++) {
      dp[i][0] = (grid[i][0] == '*' ? 0 : 1);
    } // end for
    for (int j = 1; j < n; j++) {
      dp[0][j] = (grid[0][j] == '*' ? 0 : 1);
    } // end for
    for (int i = 1; i < n; i++) {
      for (int j = 1; j < n; j++) {
        dp[i][j] =
            (grid[i][j] == '*'
                 ? 0
                 : min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1);
      } // end for
    }   // end for

    //		disp_grid (n );
    //		disp_dp (n );
    int res = 0;
    for (int i = 1; i < n; i++) {
      for (int j = 1; j < n; j++) {
        res = max(res, dp[i][j]);
      } // end for
    }   // end for
    printf("%d\n", res);
  } // end loop

  return 0;
}