Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#define int long long
#define iris 1000000007
using namespace std;

char arr[32][32];
int dis[32][32];
int id[32][32];

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m, i, j, k;
  cin >> n >> m;
  k = 0;

  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      id[i][j] = k++;

  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      cin >> arr[i][j];

  for (i = 0; i < k; i++)
    for (j = 0; j < k; j++)
      if (i != j)
        dis[i][j] = dis[j][i] = 9999;

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (i + 1 <= n)
        dis[id[i][j]][id[i + 1][j]] = dis[id[i + 1][j]][id[i][j]] = 1;
      if (i - 1 >= 1)
        dis[id[i][j]][id[i - 1][j]] = dis[id[i - 1][j]][id[i][j]] = 1;
      if (j + 1 <= m)
        dis[id[i][j]][id[i][j + 1]] = dis[id[i][j + 1]][id[i][j]] = 1;
      if (j - 1 >= 1)
        dis[id[i][j]][id[i][j - 1]] = dis[id[i][j - 1]][id[i][j]] = 1;
    }
  }

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (arr[i][j] == '#') {
        if (i + 1 <= n)
          dis[id[i][j]][id[i + 1][j]] = dis[id[i + 1][j]][id[i][j]] = 9999;
        if (i - 1 >= 1)
          dis[id[i][j]][id[i - 1][j]] = dis[id[i - 1][j]][id[i][j]] = 9999;
        if (j + 1 <= m)
          dis[id[i][j]][id[i][j + 1]] = dis[id[i][j + 1]][id[i][j]] = 9999;
        if (j - 1 >= 1)
          dis[id[i][j]][id[i][j - 1]] = dis[id[i][j - 1]][id[i][j]] = 9999;
      }
    }
  }

  for (int ouo = 0; ouo < k; ouo++) {
    for (i = 0; i < k; i++) {
      for (j = 0; j < k; j++) {
        dis[i][j] = min(dis[i][j], dis[i][ouo] + dis[ouo][j]);
      }
    }
  }
  int ans = 0;
  for (i = 0; i < k; i++) {
    for (j = 0; j < k; j++) {
      if (dis[i][j] < 9999) {
        ans = max(ans, dis[i][j]);
      }
    }
  }
  cout << ans << '\n';

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#define int long long
#define iris 1000000007
using namespace std;

char arr[32][32];
int dis[1024][1024];
int id[32][32];

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m, i, j, k;
  cin >> n >> m;
  k = 0;

  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      id[i][j] = k++;

  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      cin >> arr[i][j];

  for (i = 0; i < k; i++)
    for (j = 0; j < k; j++)
      if (i != j)
        dis[i][j] = dis[j][i] = 9999;

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (i + 1 <= n)
        dis[id[i][j]][id[i + 1][j]] = dis[id[i + 1][j]][id[i][j]] = 1;
      if (i - 1 >= 1)
        dis[id[i][j]][id[i - 1][j]] = dis[id[i - 1][j]][id[i][j]] = 1;
      if (j + 1 <= m)
        dis[id[i][j]][id[i][j + 1]] = dis[id[i][j + 1]][id[i][j]] = 1;
      if (j - 1 >= 1)
        dis[id[i][j]][id[i][j - 1]] = dis[id[i][j - 1]][id[i][j]] = 1;
    }
  }

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (arr[i][j] == '#') {
        if (i + 1 <= n)
          dis[id[i][j]][id[i + 1][j]] = dis[id[i + 1][j]][id[i][j]] = 9999;
        if (i - 1 >= 1)
          dis[id[i][j]][id[i - 1][j]] = dis[id[i - 1][j]][id[i][j]] = 9999;
        if (j + 1 <= m)
          dis[id[i][j]][id[i][j + 1]] = dis[id[i][j + 1]][id[i][j]] = 9999;
        if (j - 1 >= 1)
          dis[id[i][j]][id[i][j - 1]] = dis[id[i][j - 1]][id[i][j]] = 9999;
      }
    }
  }

  for (int ouo = 0; ouo < k; ouo++) {
    for (i = 0; i < k; i++) {
      for (j = 0; j < k; j++) {
        dis[i][j] = min(dis[i][j], dis[i][ouo] + dis[ouo][j]);
      }
    }
  }
  int ans = 0;
  for (i = 0; i < k; i++) {
    for (j = 0; j < k; j++) {
      if (dis[i][j] < 9999) {
        ans = max(ans, dis[i][j]);
      }
    }
  }
  cout << ans << '\n';

  return 0;
}