Problem p03014 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>

static const int IINF = 1 << 30;
static const long long LINF = 1LL << 60;
static const long long MOD = 1.0e+9 + 7;

template <typename T> std::vector<T> vectors(std::size_t n, T val) {
  return std::vector<T>(n, val);
}

template <typename T, typename... Args>
auto vectors(std::size_t n, Args... args) {
  return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...));
}

template <class T> inline bool chmin(T &a, const T &b) {
  return (a > b) ? a = b, true : false;
}

template <class T> inline bool chmax(T &a, const T &b) {
  return (a < b) ? a = b, true : false;
}

template <class T> inline void chadd(T &a, const T &b) {
  a += b, a %= MOD;
  // TODO minus case
}

template <class T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
  if (v.empty())
    return s;
  s << *v.begin();
  for (auto iter = v.begin() + 1; iter != v.end(); ++iter)
    if (std::is_fundamental<T>::value)
      s << " " << *iter;
    else
      s << std::endl << *iter;
  return s;
}

int main() {
  // Input
  int H, W;
  std::cin >> H >> W;
  std::vector<std::string> S(H);
  for (int i = 0; i < H; ++i)
    std::cin >> S[i];

  std::vector<std::vector<char>> B(1 + H + 1);
  B[0] = std::vector<char>(1 + W + 1, '-');
  B[H + 1] = std::vector<char>(1 + W + 1, '-');
  for (int i = 1; i <= H; ++i) {
    B[i].assign(1 + W + 1, '-');
    for (int j = 1; j <= W; ++j)
      B[i][j] = S[i - 1][j - 1];
  }

  // Main
  std::vector<std::vector<int>> yoko(1 + H + 1);
  std::vector<std::vector<int>> tate(1 + H + 1);
  for (int i = 0; i <= H + 1; ++i) {
    yoko[i].assign(1 + W + 1, 0);
    tate[i].assign(1 + H + 1, 0);
  }

  // Yoko
  for (int i = 1; i <= H; ++i) {
    std::vector<int> left;
    std::vector<int> right;
    bool on = false;
    for (int j = 0; j <= W + 1; ++j) {
      if (not on and B[i][j] == '.') {
        // Startup
        left.push_back(j);
        on = true;
      } else if (on and B[i][j] != '.') {
        // Shutdown
        right.push_back(j);
        on = false;
      }
    }
    // assert(left.size() == right.size());
    for (int k = 0; k < left.size(); ++k) {
      int l = left[k];
      int r = right[k];
      for (int j = l; j < r; ++j) {
        yoko[i][j] = r - l;
      }
    }
  }

  // Tate
  for (int j = 1; j <= W; ++j) {
    std::vector<int> left;
    std::vector<int> right;
    bool on = false;
    for (int i = 0; i <= H + 1; ++i) {
      if (not on and B[i][j] == '.') {
        left.push_back(i);
        on = true;
      } else if (on and B[i][j] != '.') {
        right.push_back(i);
        on = false;
      }
    }
    // assert(left.size() == right.size());
    for (int k = 0; k < left.size(); ++k) {
      int l = left[k];
      int r = right[k];
      for (int i = l; i < r; ++i) {
        tate[i][j] = r - l;
      }
    }
  }

  // Output
  int res = 0;
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (B[i][j] != '.')
        continue;
      chmax(res, yoko[i][j] + tate[i][j] - 1);
    }
  }
  std::cout << res << std::endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>

static const int IINF = 1 << 30;
static const long long LINF = 1LL << 60;
static const long long MOD = 1.0e+9 + 7;

template <typename T> std::vector<T> vectors(std::size_t n, T val) {
  return std::vector<T>(n, val);
}

template <typename T, typename... Args>
auto vectors(std::size_t n, Args... args) {
  return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...));
}

template <class T> inline bool chmin(T &a, const T &b) {
  return (a > b) ? a = b, true : false;
}

template <class T> inline bool chmax(T &a, const T &b) {
  return (a < b) ? a = b, true : false;
}

template <class T> inline void chadd(T &a, const T &b) {
  a += b, a %= MOD;
  // TODO minus case
}

template <class T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
  if (v.empty())
    return s;
  s << *v.begin();
  for (auto iter = v.begin() + 1; iter != v.end(); ++iter)
    if (std::is_fundamental<T>::value)
      s << " " << *iter;
    else
      s << std::endl << *iter;
  return s;
}

int main() {
  // Input
  int H, W;
  std::cin >> H >> W;
  std::vector<std::string> S(H);
  for (int i = 0; i < H; ++i)
    std::cin >> S[i];

  std::vector<std::vector<char>> B(1 + H + 1);
  B[0] = std::vector<char>(1 + W + 1, '-');
  B[H + 1] = std::vector<char>(1 + W + 1, '-');
  for (int i = 1; i <= H; ++i) {
    B[i].assign(1 + W + 1, '-');
    for (int j = 1; j <= W; ++j)
      B[i][j] = S[i - 1][j - 1];
  }

  // Main
  std::vector<std::vector<int>> yoko(1 + H + 1);
  std::vector<std::vector<int>> tate(1 + H + 1);
  for (int i = 0; i <= H + 1; ++i) {
    yoko[i].assign(1 + W + 1, 0);
    tate[i].assign(1 + W + 1, 0);
  }

  // Yoko
  for (int i = 1; i <= H; ++i) {
    std::vector<int> left;
    std::vector<int> right;
    bool on = false;
    for (int j = 0; j <= W + 1; ++j) {
      if (not on and B[i][j] == '.') {
        // Startup
        left.push_back(j);
        on = true;
      } else if (on and B[i][j] != '.') {
        // Shutdown
        right.push_back(j);
        on = false;
      }
    }
    // assert(left.size() == right.size());
    for (int k = 0; k < left.size(); ++k) {
      int l = left[k];
      int r = right[k];
      for (int j = l; j < r; ++j) {
        yoko[i][j] = r - l;
      }
    }
  }

  // Tate
  for (int j = 1; j <= W; ++j) {
    std::vector<int> left;
    std::vector<int> right;
    bool on = false;
    for (int i = 0; i <= H + 1; ++i) {
      if (not on and B[i][j] == '.') {
        left.push_back(i);
        on = true;
      } else if (on and B[i][j] != '.') {
        right.push_back(i);
        on = false;
      }
    }
    // assert(left.size() == right.size());
    for (int k = 0; k < left.size(); ++k) {
      int l = left[k];
      int r = right[k];
      for (int i = l; i < r; ++i) {
        tate[i][j] = r - l;
      }
    }
  }

  // Output
  int res = 0;
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (B[i][j] != '.')
        continue;
      chmax(res, yoko[i][j] + tate[i][j] - 1);
    }
  }
  std::cout << res << std::endl;

  return 0;
}