alexjercan/bugnet huggingface dataset:
Problem p00707 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <iostream>
using namespace std;

int main(void) {
  int W, H;
  string dp[2][70];
  string line, ans;
  while (cin >> W >> H, W) {
    for (int i = 0; i < 70; i++) {
      dp[0][i].clear();
      dp[1][i].clear();
    }
    ans.clear();
    for (int i = 0; i < H; i++) {
      cin >> line;
      for (int j = 0; j < W; j++) {
        dp[i & 1][j].clear();
        if (isdigit(line[j])) {
          if (dp[(i & 1) ^ 1][j] != "0") {
            dp[i & 1][j] = dp[(i & 1) ^ 1][j] + line[j];
          } else {
            dp[i & 1][j].push_back(line[j]);
          }
          if (j != 0 && dp[i & 1][j - 1] != "0")
            dp[i & 1][j] =
                max(dp[i & 1][j], dp[i & 1][j - 1] + line[j],
                    [](const string &a, const string &b) {
                      return a.size() == b.size() ? a < b : a.size() < b.size();
                    });
          ans = max(dp[i & 1][j], ans, [](const string &a, const string &b) {
            return a.size() == b.size() ? a < b : a.size() < b.size();
          });
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iostream>
using namespace std;

int main(void) {
  int W, H;
  string dp[2][70];
  string line, ans;
  while (cin >> W >> H, W) {
    for (int i = 0; i < 70; i++) {
      dp[0][i].clear();
      dp[1][i].clear();
    }
    ans.clear();
    for (int i = 0; i < H; i++) {
      cin >> line;
      for (int j = 0; j < W; j++) {
        if (isdigit(line[j])) {
          if (line[j] == '0') {
            dp[i & 1][j].clear();
            if (!dp[(i & 1) ^ 1][j].empty())
              dp[i & 1][j] = dp[(i & 1) ^ 1][j] + line[j];
            if (!dp[i & 1][j - 1].empty()) {
              dp[i & 1][j] = max(dp[i & 1][j], dp[i & 1][j - 1] + line[j],
                                 [](const string &a, const string &b) {
                                   return a.size() == b.size()
                                              ? a < b
                                              : a.size() < b.size();
                                 });
            }
          } else {
            dp[i & 1][j] = dp[(i & 1) ^ 1][j] + line[j];
            if (j != 0)
              dp[i & 1][j] = max(dp[i & 1][j], dp[i & 1][j - 1] + line[j],
                                 [](const string &a, const string &b) {
                                   return a.size() == b.size()
                                              ? a < b
                                              : a.size() < b.size();
                                 });
            ans = max(dp[i & 1][j], ans, [](const string &a, const string &b) {
              return a.size() == b.size() ? a < b : a.size() < b.size();
            });
          }
          // cout << dp[i&1][j] << endl;
        } else {
          // cout << line[j] << "clear\\n";
          dp[i & 1][j].clear();
        }
      }
    }
    cout << "" << ans << endl;
  }
  return 0;
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int W, H;
vector<string> vs(70);
map<int, string> MAP;
bool checked[70][70];
string calc(string a, string b) {
  if (a.length() > b.length())
    return a;
  if (a.length() < b.length())
    return b;
  else
    return a > b ? a : b;
}
string dfs(int h, int w) {
  checked[h][w] = true;
  if (h < 0 || h >= H || w < 0 || w >= W || !isdigit(vs[h][w])) {
    return "";
  }
  if (MAP.count(h * 100 + w))
    return MAP[h * 100 + w];
  string a = vs[h][w] + dfs(h + 1, w);
  string b = vs[h][w] + dfs(h, w + 1);
  MAP[100 * h + w] = calc(MAP[100 * h + w], calc(a, b));
  return MAP[100 * h + w];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> W >> H, W || H) {
    FOR(i, 0, H) cin >> vs[i];
    string ans = "";
    CLR(checked);
    MAP.clear();
    FOR(h, 0, H) {
      FOR(w, 0, W) {
        if (checked[h][w] || vs[h][w] == '0')
          continue;
        checked[h][w] = true;
        ans = calc(ans, dfs(h, w));
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int W, H;
vector<string> vs(70);
string ans;
bool checked[70][70];
bool calc(string a, string b) {
  if (a.length() > b.length())
    return true;
  if (a.length() < b.length())
    return false;
  else
    return a > b;
}
void dfs(int h, int w, string s) {
  checked[h][w] = true;
  if (h < 0 || h >= H || w < 0 || w >= W || !isdigit(vs[h][w])) {
    if (calc(s, ans))
      ans = s;
    return;
  }
  s += vs[h][w];
  dfs(h + 1, w, s);
  dfs(h, w + 1, s);
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> W >> H, W || H) {
    FOR(i, 0, H) cin >> vs[i];
    ans = "";
    CLR(checked);
    FOR(h, 0, H) {
      FOR(w, 0, W) {
        if (checked[h][w])
          continue;
        checked[h][w] = true;
        dfs(h, w, "");
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;

string dp[80][80];
string field[80];
int n, m;

bool isIn(int x, int y) { return 0 <= x && x < m && 0 <= y && y < n; }

string nmax(string a, string b) {
  if (a.size() != b.size())
    return a.size() < b.size() ? b : a;
  else
    return a < b ? b : a;
}

int main() {
  while (cin >> n >> m, n + m) {
    for (int i = 0; i < m; i++)
      cin >> field[i];
    for (int i = 0; i < 80; i++)
      for (int j = 0; j < 80; j++)
        dp[i][j] = "";
    string ret;

    for (int i = m - 1; 0 <= i; i--) {
      for (int j = n - 1; 0 <= j; j--) {
        if ('0' <= field[i][j] && field[i][j] <= '9') {
          string a, b;
          if (isIn(i + 1, j))
            a = dp[i + 1][j];
          if (isIn(i, j + 1))
            b = dp[i][j + 1];
          dp[i][j] = field[i][j] + nmax(a, b);
        }
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        while (dp[i][j][0] == '0')
          dp[i][j] = dp[i][j].substr(1);
        ret = nmax(ret, dp[i][j]);
      }
    }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<string> field(80);

int n, m;

bool use[90][90];

bool isIn(int x, int y) { return 0 <= x && x < m && 0 <= y && y < n; }

string nmax(string a, string b) {
  if (a.size() != b.size())
    return a.size() < b.size() ? b : a;
  else
    return a < b ? b : a;
}

string search(int x, int y, string num) {
  use[x][y] = true;
  if (!isIn(x, y))
    return num;
  if ('0' <= field[x][y] && field[x][y] <= '9') {
    if (!(num.size() == 0 && field[x][y] == '0'))
      num += field[x].substr(y, 1);
    num = nmax(search(x + 1, y, num), search(x, y + 1, num));
    return num;
  } else
    return num;
}

int main() {
  while (cin >> n >> m, n + m) {
    for (int i = 0; i < m; i++)
      cin >> field[i];
    for (int i = 0; i < 90; i++)
      for (int j = 0; j < 90; j++)
        use[i][j] = false;
    string ret;

    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++)
        if (!use[i][j] && '0' <= field[i][j] && field[i][j] <= '9')
          ret = nmax(ret, search(i, j, ""));
    }

    cout << ret << endl;
  }
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;

bool isdigit(char c) {
  if (c >= '0' && c <= '9')
    return true;
  else
    return false;
}

int main() {
  int w, h;
  string field[70];
  while (1) {
    cin >> w >> h;
    if (!w)
      break;
    string dp[71][71] = {};
    for (int i = 0; i < h; i++)
      cin >> field[i];
    string ma = "";
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (isdigit(field[i - 1][j - 1])) {
          string s1 = dp[i - 1][j], s2 = dp[i][j - 1];
          if (s1 == "" && s2 == "" && field[i - 1][j - 1] == '0')
            continue;
          if (s1.length() > s2.length() ||
              (s1.length() == s2.length() && s1 > s2)) {
            dp[i][j] = s1 + to_string(field[i - 1][j - 1] - '0');
          } else {
            dp[i][j] = s2 + to_string(field[i - 1][j - 1] - '0');
          }
          if (dp[i][j].length() > ma.length() ||
              (dp[i][j].length() == ma.length() && dp[i][j] > ma)) {
            ma = dp[i][j];
          }
        }
      }
    }
    cout << ma << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int w, h;
string ma;
string field[70];

bool isdigit(char c) {
  if (c >= '0' && c <= '9')
    return true;
  else
    return false;
}

void dfs(int x, int y, string s) {
  // cout << x << " " << y << " " << s;
  if (!isdigit(field[x][y]) || (x == h - 1 && y == w - 1)) {
    if (x == h - 1 && y == w - 1)
      s += field[x][y];
    if (ma.length() < s.length() || (ma.length() == s.length() && ma < s))
      ma = s;
    return;
  }
  if (x + 1 < h)
    dfs(x + 1, y, s + to_string(field[x][y] - '0'));
  if (y + 1 < w)
    dfs(x, y + 1, s + to_string(field[x][y] - '0'));
}

int main() {
  while (1) {
    cin >> w >> h;
    if (!w)
      break;
    ma = "";
    for (int i = 0; i < h; i++)
      cin >> field[i];
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if ((i >= 1 && isdigit(field[i - 1][j])) ||
            (j >= 1 && isdigit(field[i][j - 1])))
          continue;
        if (isdigit(field[i][j]))
          dfs(i, j, "");
      }
    }
    bool flag = true;
    for (int i = 0; i < ma.length(); i++) {
      if (ma[i] == '0' && flag)
        continue;
      else {
        flag = false;
        cout << ma[i];
      }
    }
    cout << endl;
  }
  return 0;
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pll> vp;
typedef vector<ll> vll;

int W, H;
string c[71];
string dp[71][71];

bool inside(int x, int y) { return 0 <= x && x < H && 0 <= y && y < W; }

string maxString(string s, string t) {
  if (s.length() > t.length())
    return s;
  if (s.length() < t.length())
    return t;
  rep(i, s.length()) {
    if (s[i] > t[i])
      return s;
    if (s[i] < t[i])
      return t;
  }
  return s;
}

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

  while (cin >> W >> H && W) {
    rep(i, H) cin >> c[i];

    rep(i, H + 1) rep(j, W + 1) dp[i][j] = "";
    string ans = "";
    rep2(i, 1, H + 1) {
      rep2(j, 1, W + 1) {
        int ci = i - 1, cj = j - 1;
        if (!isdigit(c[ci][cj]))
          continue;
        string maxS = maxString(dp[i - 1][j], dp[i][j - 1]);
        if (maxS == "" && c[ci][cj] == '0') {
          dp[i][j] = "";
        } else {
          dp[i][j] = maxS + c[ci][cj];
          ans = maxString(ans, dp[i][j]);
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pll> vp;
typedef vector<ll> vll;

int W, H;
string c[70];

string ans;

bool inside(int x, int y) { return 0 <= x && x < H && 0 <= y && y < W; }

void update(string t) {
  if (ans.length() > t.length())
    return;
  if (ans.length() < t.length()) {
    ans = t;
    return;
  }
  rep(i, ans.length()) {
    if (ans[i] > t[i])
      return;
    if (ans[i] < t[i]) {
      ans = t;
      return;
    }
  }
}

void dfs(int i, int j, string t) {
  if (!inside(i, j) || !isdigit(c[i][j])) {
    update(t);
    return;
  }

  t += c[i][j];

  dfs(i, j + 1, t);
  dfs(i + 1, j, t);
}

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

  while (cin >> W >> H && W) {
    rep(i, H) cin >> c[i];

    ans = "";
    rep(i, H) rep(j, W) {
      // avoid leading zero
      if (c[i][j] == '0')
        continue;
      // if this can't be top, then skip
      if (inside(i - 1, j) && isdigit(c[i - 1][j]) && c[i - 1][j] != '0')
        continue;
      if (inside(i, j - 1) && isdigit(c[i][j - 1]) && c[i][j - 1] != '0')
        continue;
      dfs(i, j, "");
    }
    cout << ans << endl;
  }
}
Problem p00707 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

const int WH = 4900;

char board[WH];
string dp[WH];

bool comp(const string &a, const string &b) {
  if (a.length() == b.length()) {
    return a < b;
  }
  return a.length() < b.length();
}

int main() {
  int w, h, m;
  string r;
  while (cin >> w >> h, w) {
    r = "";
    for (int i = 0; i < w * h; ++i) {
      cin >> board[i];
    }
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        dp[i * w + j] = "";
        if (i - 1 >= 0 && dp[(i - 1) * w + j] != "") {
          dp[i * w + j] = dp[(i - 1) * w + j];
        }
        if (j - 1 >= 0 && dp[i * w + (j - 1)] != "") {
          dp[i * w + j] = max(dp[i * w + j], dp[i * w + (j - 1)], comp);
        }

        if ('0' <= board[i * w + j] && board[i * w + j] <= '9') {
          if (dp[i * w + j] != "" || board[i * w + j] != '0') {
            dp[i * w + j] += board[i * w + j];
          }
        } else
          dp[i * w + j] = "";

        r = max(r, dp[i * w + j], comp);
      }
    }
    cout << r << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

const int WH = 72;

char board[WH];
string dp[WH];

bool comp(const string &a, const string &b) {
  if (a.length() == b.length()) {
    return a < b;
  }
  return a.length() < b.length();
}

int main() {
  int w, h, m;
  string r;
  while (cin >> w >> h, w) {
    r = "";
    for (int i = 0; i < w * h; ++i) {
      cin >> board[i];
    }
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        dp[i * w + j] = "";
        if (i - 1 >= 0 && dp[(i - 1) * w + j] != "") {
          dp[i * w + j] = dp[(i - 1) * w + j];
        }
        if (j - 1 >= 0 && dp[i * w + (j - 1)] != "") {
          dp[i * w + j] = max(dp[i * w + j], dp[i * w + (j - 1)], comp);
        }

        if ('0' <= board[i * w + j] && board[i * w + j] <= '9') {
          if (dp[i * w + j] != "" || board[i * w + j] != '0') {
            dp[i * w + j] += board[i * w + j];
          }
        } else
          dp[i * w + j] = "";

        r = max(r, dp[i * w + j], comp);
      }
    }
    cout << r << endl;
  }
  return 0;
}
Problem p00707 (C++) — buggy submission, verdict: Runtime Error

/**
 *  The Secret Number (PKU 2030)
 *  by Kenji Inoue, 2007-01-02
 **/

#include <iostream>
#include <string>

using namespace std;

#define isdigit(x) ('0' <= (x) && (x) <= '9')

char f[71][71];   // field
string t[71][71]; // table
int w, h;

string max(string s1, string s2) {
  if (s1.size() > s2.size()) {
    return s1;
  } else if (s1.size() < s2.size()) {
    return s2;
  }

  if (s1 > s2) {
    return s1;
  }
  return s2;
}

string rec(int x, int y) {
  if (x < 0 || y < 0) {
    return "";
  }
  if (!isdigit(f[y][x])) {
    return "";
  }
  if (t[y][x] != "-") {
    return t[y][x];
  }

  t[y][x] = max(rec(x, y - 1), rec(x - 1, y)) + f[y][x];
  if (t[y][x] == "0") {
    t[y][x] = "";
  }

  return t[y][x];
}

int main() {
  while (cin >> w >> h, (w || h)) {
    for (int y = 0; y < h; y++) {
      cin >> f[y];
      for (int x = 0; x < w; x++) {
        t[y][x] = "-";
      }
    }

    string ans = "";
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if ((y == h - 1 || !isdigit(f[y + 1][x])) &&
            (x == w - 1 || !isdigit(f[y][x + 1]))) {
          ans = max(ans, rec(x, y));
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int w, h;
string s[51], dp[51][51];
bool is_num(char c) { return '0' <= c && c <= '9'; }

string str_max(const string &s1, const string &s2) {
  if (s1.size() == s2.size()) {
    return (s1 > s2) ? s1 : s2;
  }
  return (s1.size() > s2.size()) ? s1 : s2;
}

string dfs(int i, int j) {
  string res = "";

  // l¶áÈ¢ê
  if (!is_num(s[i][j]))
    return res;
  // DP
  if (dp[i][j] != "")
    return dp[i][j];

  if (i + 1 >= h && j + 1 >= w) {
    // EÉàºÉàißÈ¢
    res += s[i][j];
  } else if (i + 1 >= h) {
    // EÉ¾¯ißé
    res += s[i][j] + dfs(i, j + 1);
  } else if (j + 1 >= w) {
    // ºÉ¾¯ißé
    res += s[i][j] + dfs(i + 1, j);
  } else {
    // Ç¿çÉàißé
    res = s[i][j] + str_max(dfs(i, j + 1), dfs(i + 1, j));
  }
  // ªÉ0ªÂ¢Ä¢éêÍæé
  if (i == 0 && j == 0) {
    if (res[0] == '0')
      res.erase(0, 1);
  } else if (i == 0) {
    if (!is_num(s[i][j - 1]) && res[0] == '0')
      res.erase(0, 1);
  } else if (j == 0) {
    if (!is_num(s[i - 1][j]) && res[0] == '0')
      res.erase(0, 1);
  } else {
    if (!is_num(s[i - 1][j]) && !is_num(s[i][j - 1]))
      res.erase(0, 1);
  }

  return dp[i][j] = res;
}

void solve() {
  for (int i = 0; i < h; i++)
    cin >> s[i];
  // init
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      dp[i][j] = "";

  // search
  string largest = "";

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (dp[i][j] == "")
        largest = str_max(dfs(i, j), largest);
  /*
  for(int i=0;i<h;i++) {
    for(int j=0;j<w;j++) cout<<"["<<dp[i][j]<<"]";
    cout<<endl;
  }*/

  cout << largest << endl;
}

int main(int argc, char const *argv[]) {
  while (cin >> w >> h, w || h) {
    solve();
  }
  return 0;
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;

int H, W;
char S[71][71];
string maxs, DP[100][100];

int main() {
  while (1) {
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    maxs.erase(maxs.begin(), maxs.end());
    for (int i = 1; i < H + 1; i++) {
      for (int j = 1; j < W + 1; j++) {
        cin >> S[i][j];
        DP[i][j].erase(DP[i][j].begin(), DP[i][j].end());
      }
    }
    for (int i = 1; i < H + 1; i++) {
      for (int j = 1; j < W + 1; j++) {
        if (S[i][j] == '0' && DP[i - 1][j].empty() && DP[i][j - 1].empty())
          continue;
        if (S[i][j] >= '0' && S[i][j] <= '9') {
          if (DP[i - 1][j].length() > DP[i][j - 1].length())
            DP[i][j] = DP[i - 1][j] + S[i][j];
          else if (DP[i - 1][j].length() < DP[i][j - 1].length())
            DP[i][j] = DP[i][j - 1] + S[i][j];
          else if (DP[i - 1][j] > DP[i][j - 1])
            DP[i][j] = DP[i - 1][j] + S[i][j];
          else
            DP[i][j] = DP[i][j - 1] + S[i][j];
          if (maxs.length() < DP[i][j].length())
            maxs = DP[i][j];
          else if (maxs.length() == DP[i][j].length() && maxs < DP[i][j])
            maxs = DP[i][j];
        }
      }
    }
    cout << maxs << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int H, W;
char S[71][71];
string maxs, s;

void dfs(int h, int w) {
  s = s + S[h][w];
  if (maxs.length() < s.length())
    maxs = s;
  else if (maxs.length() == s.length() && maxs < s)
    maxs = s;
  if (h + 1 < H) {
    if ('0' <= S[h + 1][w] && S[h + 1][w] <= '9') {
      dfs(h + 1, w);
      s.erase(s.begin() + s.length() - 1);
    }
  }
  if (w + 1 < W) {
    if ('0' <= S[h][w + 1] && S[h][w + 1] <= '9') {
      dfs(h, w + 1);
      s.erase(s.begin() + s.length() - 1);
    }
  }
}
int main() {
  while (1) {
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cin >> S[i][j];
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (S[i][j] >= '1' && S[i][j] <= '9') {
          s.erase(s.begin(), s.end());
          dfs(i, j);
        }
      }
    }
    cout << maxs << endl;
    maxs.erase(maxs.begin(), maxs.end());
  }
  return 0;
}