Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 50
// #define int long long
#define INF 1e9
#define rep(i, l, r) for (register int i = l; i <= r; ++i)
using namespace std;
inline int read() {
  register char cc = getchar();
  register int cn(0), flus(1);
  while (cc > '9' || cc < '0') {
    if (cc == '-')
      flus = -flus;
    cc = getchar();
  }
  while (cc >= '0' && cc <= '9') {
    cn = cn * 10 + cc - '0';
    cc = getchar();
  }
  return cn * flus;
}
int e[N][N];
int tmp[N][N];
int a, b;
inline void floyd() {
  rep(k, 1, a * b) {
    rep(i, 1, a * b) {
      rep(j, 1, a * b) { e[i][j] = min(e[i][j], e[i][k] + e[k][j]); }
    }
  }
}
signed main() {
  a = read();
  b = read();
  rep(i, 1, a * b) {
    rep(j, 1, a * b) {
      if (i != j)
        e[i][j] = 1e9;
    }
  }
  rep(i, 1, a) {
    char s[N];
    scanf("%s", s + 1);
    rep(j, 1, b) {
      if (s[j] == '.')
        tmp[i][j] = 1;
    }
  }
  rep(i, 1, a) {
    rep(j, 1, b) {
      int k = i - 1;
      if (tmp[i][j]) {
        // printf("i = %d j = %d\n",i,j);
        // printf("tmp[i][j - 1] = %d\n",tmp[i][j - 1]);
        if (tmp[i][j - 1]) {
          e[k * b + j - 1][k * b + j] = 1;
          e[k * b + j][k * b + j - 1] = 1;
          // printf("added(%d,%d)\n",i * b + j - 1,i * b + j);
        }
        if (tmp[i][j + 1]) {
          e[k * b + j + 1][k * b + j] = 1;
          e[k * b + j][k * b + j + 1] = 1;
        }
        if (tmp[i + 1][j]) {
          e[(k + 1) * b + j][k * b + j] = 1;
          e[k * b + j][(k + 1) * b + j] = 1;
        }
        if (tmp[i - 1][j]) {
          e[(k - 1) * b + j][k * b + j] = 1;
          e[k * b + j][(k - 1) * b + j] = 1;
        }
      }
    }
  }
  floyd();
  int _max = 0;
  /*rep(i, 1, a * b)
  {
          rep(j, 1, b * a)
          {
                  printf("%d ",e[i][j]);
          }
          puts("");
  }*/
  rep(i, 1, a * b) {
    rep(j, 1, b * a) {
      if (e[i][j] > _max && e[i][j] < 1e9)
        _max = e[i][j];
    }
  }
  printf("%d\n", _max);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define N 500
// #define int long long
#define INF 1e9
#define rep(i, l, r) for (register int i = l; i <= r; ++i)
using namespace std;
inline int read() {
  register char cc = getchar();
  register int cn(0), flus(1);
  while (cc > '9' || cc < '0') {
    if (cc == '-')
      flus = -flus;
    cc = getchar();
  }
  while (cc >= '0' && cc <= '9') {
    cn = cn * 10 + cc - '0';
    cc = getchar();
  }
  return cn * flus;
}
int e[N][N];
int tmp[N][N];
int a, b;
inline void floyd() {
  rep(k, 1, a * b) {
    rep(i, 1, a * b) {
      rep(j, 1, a * b) { e[i][j] = min(e[i][j], e[i][k] + e[k][j]); }
    }
  }
}
signed main() {
  a = read();
  b = read();
  rep(i, 1, a * b) {
    rep(j, 1, a * b) {
      if (i != j)
        e[i][j] = 1e9;
    }
  }
  rep(i, 1, a) {
    char s[N];
    scanf("%s", s + 1);
    rep(j, 1, b) {
      if (s[j] == '.')
        tmp[i][j] = 1;
    }
  }
  rep(i, 1, a) {
    rep(j, 1, b) {
      int k = i - 1;
      if (tmp[i][j]) {
        // printf("i = %d j = %d\n",i,j);
        // printf("tmp[i][j - 1] = %d\n",tmp[i][j - 1]);
        if (tmp[i][j - 1]) {
          e[k * b + j - 1][k * b + j] = 1;
          e[k * b + j][k * b + j - 1] = 1;
          // printf("added(%d,%d)\n",i * b + j - 1,i * b + j);
        }
        if (tmp[i][j + 1]) {
          e[k * b + j + 1][k * b + j] = 1;
          e[k * b + j][k * b + j + 1] = 1;
        }
        if (tmp[i + 1][j]) {
          e[(k + 1) * b + j][k * b + j] = 1;
          e[k * b + j][(k + 1) * b + j] = 1;
        }
        if (tmp[i - 1][j]) {
          e[(k - 1) * b + j][k * b + j] = 1;
          e[k * b + j][(k - 1) * b + j] = 1;
        }
      }
    }
  }
  floyd();
  int _max = 0;
  /*rep(i, 1, a * b)
  {
          rep(j, 1, b * a)
          {
                  printf("%d ",e[i][j]);
          }
          puts("");
  }*/
  rep(i, 1, a * b) {
    rep(j, 1, b * a) {
      if (e[i][j] > _max && e[i][j] < 1e9)
        _max = e[i][j];
    }
  }
  printf("%d\n", _max);
  return 0;
}