Problem p03014 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;

int H, W, mr[2000][2000], mc[2000][2000];
string S[2000];

int dr(int n, int m) {
  int num;

  if (mr[n][m] != -1) {
    return mr[n][m];
  }

  if (S[n][m] == '#') {
    mr[n][m] = 0;
    return 0;
  }

  if (n > 0) {
    if (S[n - 1][m] == '.') {
      mr[n][m] = dr(n - 1, m);
      return mr[n][m];
    } else {
      num = 0;
      while (S[n + num][m] == '.' && n + num < H) {
        num++;
      }
      mr[n][m] = num;
      return num;
    }
  } else {
    num = 0;
    while (S[n + num][m] == '.' && n + num < H) {
      num++;
    }
    mr[n][m] = num;
    return num;
  }
}

int dc(int n, int m) {
  int num;

  if (mc[n][m] != -1) {
    return mc[n][m];
  }

  if (S[n][m] == '#') {
    mc[n][m] = 0;
    return 0;
  }

  if (m > 0) {
    if (S[n][m - 1] == '.') {
      mc[n][m] = dc(n, m - 1);
      return mc[n][m];
    } else {
      num = 0;
      while (S[n][m + num] == '.' && m + num < W) {
        num++;
      }
      mc[n][m] = num;
      return num;
    }
  } else {
    num = 0;
    while (S[n][m + num] == '.' && m + num < W) {
      num++;
    }
    mc[n][m] = num;
    return num;
  }
}

int main() {

  cin >> H >> W;
  int i, j, k, ans = 0;

  for (i = 0; i < H; i++) {
    cin >> S[i];
  }
  /*
  for(i=0;i<H;i++)
  {
    cout << S[i] << endl;
  }

  for(i=0;i<H;i++)
  {
    for(j=0;j<W;j++)
    {
      cout << S[i][j] ;

    }cout <<  endl;
  }
  */
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      mc[i][j] = -1;
      mr[i][j] = -1;
    }
  }

  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      ans = max(ans, dr(i, j) + dc(i, j) - 1);
    }
  }

  //  cout << dr(1,1) << endl << dc(1,1) << endl;
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;

int H, W, mr[2000][2000], mc[2000][2000];
string S[2001];

int dr(int n, int m) {
  int num;

  if (mr[n][m] != -1) {
    return mr[n][m];
  }

  if (S[n][m] == '#') {
    mr[n][m] = 0;
    return 0;
  }

  if (n > 0) {
    if (S[n - 1][m] == '.') {
      mr[n][m] = dr(n - 1, m);
      return mr[n][m];
    } else {
      num = 0;
      while (S[n + num][m] == '.' && n + num < H) {
        num++;
      }
      mr[n][m] = num;
      return num;
    }
  } else {
    num = 0;
    while (S[n + num][m] == '.' && n + num < H) {
      num++;
    }
    mr[n][m] = num;
    return num;
  }
}

int dc(int n, int m) {
  int num;

  if (mc[n][m] != -1) {
    return mc[n][m];
  }

  if (S[n][m] == '#') {
    mc[n][m] = 0;
    return 0;
  }

  if (m > 0) {
    if (S[n][m - 1] == '.') {
      mc[n][m] = dc(n, m - 1);
      return mc[n][m];
    } else {
      num = 0;
      while (S[n][m + num] == '.' && m + num < W) {
        num++;
      }
      mc[n][m] = num;
      return num;
    }
  } else {
    num = 0;
    while (S[n][m + num] == '.' && m + num < W) {
      num++;
    }
    mc[n][m] = num;
    return num;
  }
}

int main() {

  cin >> H >> W;
  int i, j, k, ans = 0;

  for (i = 0; i < H; i++) {
    cin >> S[i];
  }
  /*
  for(i=0;i<H;i++)
  {
    cout << S[i] << endl;
  }

  for(i=0;i<H;i++)
  {
    for(j=0;j<W;j++)
    {
      cout << S[i][j] ;

    }cout <<  endl;
  }
  */
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      mc[i][j] = -1;
      mr[i][j] = -1;
    }
  }

  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      ans = max(ans, dr(i, j) + dc(i, j) - 1);
    }
  }

  //  cout << dr(1,1) << endl << dc(1,1) << endl;
  cout << ans;
}