Problem p03361 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
typedef long long ll;
const double PI = 3.141592654;
#define rep(i, x) for (int(i) = 0; (i) < (ll)(x); (i)++)
#define all(s) (s).begin(), (s).end()
#define allv(v) (v).begin(), (v).end()
#define allvr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define sz s.size()
#define ssz ss.size()
#define vz v.size()
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
int dx[] = {0, 0, -1, 1, -1, 1, -1, 1};
int dy[] = {1, -1, 0, 0, 1, 1, -1, -1};
// x = atoill(s.c_str())
void fast() {
#ifndef ONLINE_JUDGE
  /*

  **    ** ** **    *********   ** ** **
  **    **          **     **   **
  **    **          *********   **
  **    **          **          **
  **    ** ** **    **          ** ** **
  */
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(NULL), cout.tie(NULL);
}
int x, y;
bool valid(int i, int j) { return (i < x && i >= 0 && j < y && j >= 0); }
int main() {
  fast();
  char arr[51][51];
  cin >> x >> y;
  rep(i, x) { rep(j, y) cin >> arr[i][j]; }
  rep(i, x) {
    rep(j, y) {
      int c = 0;
      if (arr[i][j] == '#') {
        bool t = 0;
        for (int k = 0; k < 4; k++) {
          int tox = i + dx[k];
          int toy = j + dy[k];
          if (valid(tox, toy)) {
            if (arr[tox][toy] == '#')
              t = 1;
          }
        }
        if (t == 0) {
          cout << "No";
          return 0;
        }
      }
    }
  }
  cout << "Yes";
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
typedef long long ll;
const double PI = 3.141592654;
#define rep(i, x) for (int(i) = 0; (i) < (ll)(x); (i)++)
#define all(s) (s).begin(), (s).end()
#define allv(v) (v).begin(), (v).end()
#define allvr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define sz s.size()
#define ssz ss.size()
#define vz v.size()
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
int dx[] = {0, 0, -1, 1, -1, 1, -1, 1};
int dy[] = {1, -1, 0, 0, 1, 1, -1, -1};
// x = atoill(s.c_str())
void fast() {
#ifndef ONLINE_JUDGE
  /*

  **    ** ** **    *********   ** ** **
  **    **          **     **   **
  **    **          *********   **
  **    **          **          **
  **    ** ** **    **          ** ** **
  */
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(NULL), cout.tie(NULL);
}
int x, y;
bool valid(int i, int j) { return (i < x && i >= 0 && j < y && j >= 0); }
int main() {
  fast();
  char arr[51][51];
  cin >> x >> y;
  rep(i, x) { rep(j, y) cin >> arr[i][j]; }
  rep(i, x) {
    rep(j, y) {
      int c = 0;
      if (arr[i][j] == '#') {
        bool t = 0;
        for (int k = 0; k < 4; k++) {
          int tox = i + dx[k];
          int toy = j + dy[k];
          if (valid(tox, toy)) {
            if (arr[tox][toy] == '#')
              t = 1;
          }
        }
        if (t == 0) {
          cout << "No";
          return 0;
        }
      }
    }
  }
  cout << "Yes";
  return 0;
}