Problem p02803 (C++)

Buggy submission (status: Runtime Error):
// --------------------------------------------------<TEMPLATE>--------------------------------------------------
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN NEEDED)\
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")\
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

// -------------------</optimizations>--------------------

// ---------------<Headers and namespaces>----------------
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
*/

// ---------------</Headers and namespaces>---------------

// -----------------<Defines and typedefs>----------------
 // typedef tree<int,null_type,less<int>,rb_tree_tag, \
tree_order_statistics_node_update> indexed_set;
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the iterator to kth largest element.(0-based)

typedef long double LD;
typedef long long ll;
#define int ll
#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define all(v) v.begin(), v.end()
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
#define F first
#define S second
#define PQ(type) priority_queue<type>
#define PQD(type) priority_queue<type, vector<type>, greater<type>>
#define ITR ::iterator it
#define WL(t) while (t--)
#define SZ(x) ((int)(x).size())
#define runtime() ((double)clock() / CLOCKS_PER_SEC)
#define TR(container, it)                                                      \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)
#define sqr(x) ((x) * (x))
#define inrange(i, a, b) ((i >= min(a, b)) && (i <= max(a, b)))

// -----<SCANF>-----
#define sfi(x) scanf("%d", &x);
#define sfi2(x, y) scanf("%d%d", &x, &y);
#define sfi3(x, y, z) scanf("%d%d%d", &x, &y, &z);

#define sfl(x) scanf("%lld", &x);
#define sfl2(x, y) scanf("%lld%lld", &x, &y);
#define sfl3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define sfl4(x, y, z, x1) scanf("%lld%lld%lld%lld", &x, &y, &z, &x1);
#define sfl5(x, y, z, x1, y1)                                                  \
  scanf("%lld%lld%lld%lld%lld", &x, &y, &z, &x1, &y1);
#define sfl6(x, y, z, x1, y1, z1)                                              \
  scanf("%lld%lld%lld%lld%lld%lld", &x, &y, &z, &x1, &y1, &z1);

#define sfs(x) scanf("%s", x);
#define sfs2(x, y) scanf("%s%s", x, y);
#define sfs3(x, y, z) scanf("%s%s%s", x, y, z);
// ----</SCANF>-----

// ----<PRINTF>-----
#define pfi(x) printf("%d\n", x);
#define pfi2(x, y) printf("%d %d\n", x, y);
#define pfi3(x, y, z) printf("%d %d %d\n", x, y, z);

#define pfl(x) printf("%lld\n", x);
#define pfl2(x, y) printf("%lld %lld\n", x, y);
#define pfl3(x, y, z) printf("%lld %lld %lld\n", x, y, z);

#define pfs(x) printf("%s\n", x);
#define pfs2(x, y) printf("%s %s\n", x, y);
#define pfs3(x, y, z) printf("%s %s %s\n", x, y, z);

#define pwe(x) printf("%lld ", x); // print without end
// ----</PRINTF>----

#define FLSH fflush(stdout)
#define fileIO(name)                                                           \
  freopen(name ".in", "r", stdin);                                             \
  freopen(name ".out", "w", stdout);
#define PRECISION(x) cout << fixed << setprecision(x);
#define FAST_IO                                                                \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

// ----------------</Defines and typedefs>----------------

// -------------------<Debugging stuff>-------------------
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

// ------------------</Debugging stuff>-------------------

// ------------------------<Consts>-----------------------
const int MAXN = 1000005;
const int SQRTN = 1003;
const int LOGN = 22;
const double PI = acos(-1);

#ifdef int
const int INF = 1e16;
#else
const int INF = 1e9;
#endif

const int MOD = 1000000007;
const int FMOD = 998244353;
const double eps = 1e-9;

// -----------------------</Consts>-----------------------

// -------------------------<RNG>-------------------------
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
#define SHUF(v) shuffle(all(v), RNG);
// Use mt19937_64 for 64 bit random numbers.

// ------------------------</RNG>-------------------------

// -----------------<Modular Arithmetic>------------------
template <typename T> T gcd(T a, T b) { return (b ? __gcd(a, b) : a); }
template <typename T> T lcm(T a, T b) { return (a * (b / gcd(a, b))); }
int add(int a, int b, int c = MOD) {
  int res = a + b;
  return (res >= c ? res - c : res);
}
int mod_neg(int a, int b, int c = MOD) {
  int res;
  if (abs(a - b) < c)
    res = a - b;
  else
    res = (a - b) % c;
  return (res < 0 ? res + c : res);
}
int mul(int a, int b, int c = MOD) {
  ll res = (ll)a * b;
  return (res >= c ? res % c : res);
}
ll mulmod(ll a, ll b, ll m = MOD) {
  ll q = (ll)(((LD)a * (LD)b) / (LD)m);
  ll r = a * b - q * m;
  if (r > m)
    r %= m;
  if (r < 0)
    r += m;
  return r;
}
template <typename T> T expo(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T> T power(T e, T n, T m = MOD) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = mul(x, p, m);
    p = mul(p, p, m);
    n >>= 1;
  }
  return x;
}
template <typename T> T extended_euclid(T a, T b, T &x, T &y) {
  T xx = 0, yy = 1;
  y = 0;
  x = 1;
  while (b) {
    T q = a / b, t = b;
    b = a % b;
    a = t;
    t = xx;
    xx = x - q * xx;
    x = t;
    t = yy;
    yy = y - q * yy;
    y = t;
  }
  return a;
}
template <typename T> T mod_inverse(T a, T n = MOD) {
  T x, y, z = 0;
  T d = extended_euclid(a, n, x, y);
  return (d > 1 ? -1 : mod_neg(x, z, n));
}

// -----------------</Modular Arithmetic>-----------------
// --------------------------------------------------</TEMPLATE>--------------------------------------------------

void solvethetestcase();

signed main() {
  // (UNCOMMENT FOR CIN/COUT) \
	FAST_IO
  PRECISION(10)

  int t = 1;
  // (UNCOMMENT FOR MULTIPLE TEST CASES) \
	sfl(t);
  FOR(testcase, 1, t + 1) {
    // (UNCOMMENT FOR CODEJAM) \
		printf("Case #%lld: ",testcase);
    solvethetestcase();
  }
}

int n, m;
char a[25][25];

int dist[505][505];

int conv(int i, int j) {
  i--;
  j--;
  return i * m + j;
}

void solvethetestcase() {
  sfl2(n, m) REP(i, 25) {
    REP(j, 25) { a[i][j] = '#'; }
  }
  FOR(i, 1, n + 1) {
    FOR(j, 1, m + 1) { cin >> a[i][j]; }
  }
  REP(i, 405) {
    REP(j, 405) { dist[i][j] = INF; }
    if (a[i / m + 1][(i % m) + 1] == '.')
      dist[i][i] = 0;
  }
  FOR(i, 1, n + 1) {
    FOR(j, 1, m + 1) {
      if (a[i][j] == '.' and a[i + 1][j] == '.') {
        dist[conv(i, j)][conv(i + 1, j)] = 1;
        dist[conv(i + 1, j)][conv(i, j)] = 1;
      }
      if (a[i][j] == '.' and a[i][j + 1] == '.') {
        dist[conv(i, j)][conv(i, j + 1)] = 1;
        dist[conv(i, j + 1)][conv(i, j)] = 1;
      }
    }
  }
  REP(k, 405) {
    REP(i, 405) {
      REP(j, 405) { remin(dist[i][j], dist[i][k] + dist[k][j]); }
    }
  }
  int ans = 0;
  REP(i, 405) {
    REP(j, 405) {
      if (dist[i][j] != INF)
        remax(ans, dist[i][j]);
    }
  }
  pfl(ans)
}


Fixed submission:
// --------------------------------------------------<TEMPLATE>--------------------------------------------------
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN NEEDED)\
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")\
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

// -------------------</optimizations>--------------------

// ---------------<Headers and namespaces>----------------
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
*/

// ---------------</Headers and namespaces>---------------

// -----------------<Defines and typedefs>----------------
 // typedef tree<int,null_type,less<int>,rb_tree_tag, \
tree_order_statistics_node_update> indexed_set;
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the iterator to kth largest element.(0-based)

typedef long double LD;
typedef long long ll;
#define int ll
#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define all(v) v.begin(), v.end()
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
#define F first
#define S second
#define PQ(type) priority_queue<type>
#define PQD(type) priority_queue<type, vector<type>, greater<type>>
#define ITR ::iterator it
#define WL(t) while (t--)
#define SZ(x) ((int)(x).size())
#define runtime() ((double)clock() / CLOCKS_PER_SEC)
#define TR(container, it)                                                      \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)
#define sqr(x) ((x) * (x))
#define inrange(i, a, b) ((i >= min(a, b)) && (i <= max(a, b)))

// -----<SCANF>-----
#define sfi(x) scanf("%d", &x);
#define sfi2(x, y) scanf("%d%d", &x, &y);
#define sfi3(x, y, z) scanf("%d%d%d", &x, &y, &z);

#define sfl(x) scanf("%lld", &x);
#define sfl2(x, y) scanf("%lld%lld", &x, &y);
#define sfl3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define sfl4(x, y, z, x1) scanf("%lld%lld%lld%lld", &x, &y, &z, &x1);
#define sfl5(x, y, z, x1, y1)                                                  \
  scanf("%lld%lld%lld%lld%lld", &x, &y, &z, &x1, &y1);
#define sfl6(x, y, z, x1, y1, z1)                                              \
  scanf("%lld%lld%lld%lld%lld%lld", &x, &y, &z, &x1, &y1, &z1);

#define sfs(x) scanf("%s", x);
#define sfs2(x, y) scanf("%s%s", x, y);
#define sfs3(x, y, z) scanf("%s%s%s", x, y, z);
// ----</SCANF>-----

// ----<PRINTF>-----
#define pfi(x) printf("%d\n", x);
#define pfi2(x, y) printf("%d %d\n", x, y);
#define pfi3(x, y, z) printf("%d %d %d\n", x, y, z);

#define pfl(x) printf("%lld\n", x);
#define pfl2(x, y) printf("%lld %lld\n", x, y);
#define pfl3(x, y, z) printf("%lld %lld %lld\n", x, y, z);

#define pfs(x) printf("%s\n", x);
#define pfs2(x, y) printf("%s %s\n", x, y);
#define pfs3(x, y, z) printf("%s %s %s\n", x, y, z);

#define pwe(x) printf("%lld ", x); // print without end
// ----</PRINTF>----

#define FLSH fflush(stdout)
#define fileIO(name)                                                           \
  freopen(name ".in", "r", stdin);                                             \
  freopen(name ".out", "w", stdout);
#define PRECISION(x) cout << fixed << setprecision(x);
#define FAST_IO                                                                \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

// ----------------</Defines and typedefs>----------------

// -------------------<Debugging stuff>-------------------
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

// ------------------</Debugging stuff>-------------------

// ------------------------<Consts>-----------------------
const int MAXN = 1000005;
const int SQRTN = 1003;
const int LOGN = 22;
const double PI = acos(-1);

#ifdef int
const int INF = 1e16;
#else
const int INF = 1e9;
#endif

const int MOD = 1000000007;
const int FMOD = 998244353;
const double eps = 1e-9;

// -----------------------</Consts>-----------------------

// -------------------------<RNG>-------------------------
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
#define SHUF(v) shuffle(all(v), RNG);
// Use mt19937_64 for 64 bit random numbers.

// ------------------------</RNG>-------------------------

// -----------------<Modular Arithmetic>------------------
template <typename T> T gcd(T a, T b) { return (b ? __gcd(a, b) : a); }
template <typename T> T lcm(T a, T b) { return (a * (b / gcd(a, b))); }
int add(int a, int b, int c = MOD) {
  int res = a + b;
  return (res >= c ? res - c : res);
}
int mod_neg(int a, int b, int c = MOD) {
  int res;
  if (abs(a - b) < c)
    res = a - b;
  else
    res = (a - b) % c;
  return (res < 0 ? res + c : res);
}
int mul(int a, int b, int c = MOD) {
  ll res = (ll)a * b;
  return (res >= c ? res % c : res);
}
ll mulmod(ll a, ll b, ll m = MOD) {
  ll q = (ll)(((LD)a * (LD)b) / (LD)m);
  ll r = a * b - q * m;
  if (r > m)
    r %= m;
  if (r < 0)
    r += m;
  return r;
}
template <typename T> T expo(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T> T power(T e, T n, T m = MOD) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = mul(x, p, m);
    p = mul(p, p, m);
    n >>= 1;
  }
  return x;
}
template <typename T> T extended_euclid(T a, T b, T &x, T &y) {
  T xx = 0, yy = 1;
  y = 0;
  x = 1;
  while (b) {
    T q = a / b, t = b;
    b = a % b;
    a = t;
    t = xx;
    xx = x - q * xx;
    x = t;
    t = yy;
    yy = y - q * yy;
    y = t;
  }
  return a;
}
template <typename T> T mod_inverse(T a, T n = MOD) {
  T x, y, z = 0;
  T d = extended_euclid(a, n, x, y);
  return (d > 1 ? -1 : mod_neg(x, z, n));
}

// -----------------</Modular Arithmetic>-----------------
// --------------------------------------------------</TEMPLATE>--------------------------------------------------

void solvethetestcase();

signed main() {
  // (UNCOMMENT FOR CIN/COUT) \
	FAST_IO
  PRECISION(10)

  int t = 1;
  // (UNCOMMENT FOR MULTIPLE TEST CASES) \
	sfl(t);
  FOR(testcase, 1, t + 1) {
    // (UNCOMMENT FOR CODEJAM) \
		printf("Case #%lld: ",testcase);
    solvethetestcase();
  }
}

int n, m;
char a[25][25];

int dist[505][505];

int conv(int i, int j) {
  i--;
  j--;
  return i * m + j;
}

void solvethetestcase() {
  sfl2(n, m) REP(i, 25) {
    REP(j, 25) { a[i][j] = '#'; }
  }
  FOR(i, 1, n + 1) {
    FOR(j, 1, m + 1) { cin >> a[i][j]; }
  }
  REP(i, 405) {
    REP(j, 405) { dist[i][j] = INF; }
    if ((i / m + 1 <= n) and (i % m + 1 <= m) and
        a[i / m + 1][(i % m) + 1] == '.')
      dist[i][i] = 0;
  }
  FOR(i, 1, n + 1) {
    FOR(j, 1, m + 1) {
      if (a[i][j] == '.' and a[i + 1][j] == '.') {
        dist[conv(i, j)][conv(i + 1, j)] = 1;
        dist[conv(i + 1, j)][conv(i, j)] = 1;
      }
      if (a[i][j] == '.' and a[i][j + 1] == '.') {
        dist[conv(i, j)][conv(i, j + 1)] = 1;
        dist[conv(i, j + 1)][conv(i, j)] = 1;
      }
    }
  }
  REP(k, 405) {
    REP(i, 405) {
      REP(j, 405) { remin(dist[i][j], dist[i][k] + dist[k][j]); }
    }
  }
  int ans = 0;
  REP(i, 405) {
    REP(j, 405) {
      if (dist[i][j] != INF)
        remax(ans, dist[i][j]);
    }
  }
  pfl(ans)
}