alexjercan/bugnet huggingface dataset:
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int dx[] = {1, 0};
const int dy[] = {0, 1};

vector<string> board(80);
string memo[80][80];
int W, H;

inline bool isDigit(int x, int y) {
  if ('0' <= board[y][x] && board[y][x] <= '9')
    return true;
  else
    return false;
}

string Rec(int x, int y) {
  if (memo[y][x].size() != 0)
    return memo[y][x];

  string s, tmp;
  for (int d = 0; d < 2; ++d) {
    int nx = x + dx[d];
    int ny = y + dy[d];

    if (nx < 0 || nx >= W || ny < 0 || ny >= H || !isDigit(nx, ny))
      continue;

    tmp = Rec(nx, ny);
    if (tmp.size() > s.size() || (tmp.size() == s.size() && tmp > s))
      s = tmp;
  }

  return memo[y][x] = board[y][x] + s;
}

int main() {
  while (cin >> W >> H, W || H) {
    // Input
    for (int i = 0; i < H; ++i)
      cin >> board[i];

    // Init
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        memo[i][j] = "";

    // Solve
    string tmp, ans;
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        if (isDigit(j, i)) {
          tmp = Rec(j, i);
          int index = 0;
          while (tmp[index] == '0')
            index++;
          tmp = tmp.substr(index);

          if (tmp.size() > ans.size() ||
              (tmp.size() == ans.size() && tmp > ans))
            ans = tmp;
        }

    // Output
    cout << ans << endl;
    //        cout << ans.substr(index) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

const int dx[] = {1, 0};
const int dy[] = {0, 1};

vector<string> board(70);
bool visit[70][70];
int W, H;
int len;
string ans;
int cnt;

inline bool isDigit(int x, int y) {
  if ('0' <= board[y][x] && board[y][x] <= '9')
    return true;
  else
    return false;
}

void Rec(int x, int y, string s) {
  s += board[y][x];
  visit[y][x] = true;
  bool branch = false;
  for (int d = 0; d < 2; ++d) {
    int nx = x + dx[d];
    int ny = y + dy[d];

    if (nx < 0 || nx >= W || ny < 0 || ny >= H || !isDigit(nx, ny))
      continue;
    if ((W - nx - 1) * (H - ny - 1) + s.size() + 3 < len)
      continue;
    branch = true;
    Rec(nx, ny, s);
  }

  if (!branch && s.size() > len || (s.size() == len && s > ans)) {
    ans = s;
    len = s.size();
  }
}

int main() {
  while (cin >> W >> H, W || H) {
    len = 0;
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        visit[i][j] = false;

    for (int i = 0; i < H; ++i)
      cin >> board[i];

    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        if (!visit[i][j] && isDigit(j, i))
          Rec(j, i, "");
    int index = 0;
    while (ans[index] == '0')
      index++;
    cout << ans.substr(index) << endl;
  }

  return 0;
}
Problem p00707 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
string dp[70][70], m[70];
int h, w;
string dfs(int y, int x, string s) {
  if (dp[y][x].size() > s.size())
    return dp[y][x];
  if (dp[y][x].size() == s.size() && dp[y][x] > s)
    return dp[y][x];
  string p1, p2;
  if (y + 1 < h && '0' <= m[y + 1][x] && m[y + 1][x] <= '9')
    p1 = dfs(y + 1, x, "");
  p1 = m[y][x] + p1;
  if (x + 1 < w && '0' <= m[y][x + 1] && m[y][x + 1] <= '9')
    p2 = dfs(y, x + 1, "");
  p2 = m[y][x] + p2;
  if (p1.size() > p2.size())
    return dp[y][x] = p1;
  else if (p1.size() == p2.size() && p1 > p2)
    return dp[y][x] = p1;
  else
    return dp[y][x] = p2;
}
int main() {
  while (cin >> w >> h, w) {
    r(i, h) r(j, w) dp[i][j] = "";
    string p = "";
    r(i, h) cin >> m[i];
    r(i, h) r(j, w) if ('1' <= m[i][j] && m[i][j] <= '9') {
      string q = "";
      string t = dfs(i, j, q + m[i][j]);
      if (t.size() > p.size())
        p = t;
      else if (t.size() == p.size())
        p = max(t, p);
    }
    cout << p << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define mk make_pair
using namespace std;
int dx[] = {1, 0}, dy[] = {0, 1};
int main() {
  int w, h;
  while (cin >> w >> h, w) {
    string s[h], t = "";
    bool p[h][w];
    r(i, h) r(j, w) p[i][j] = 0;
    r(i, h) cin >> s[i];
    r(i, h) r(j, w) if (s[i][j] >= '1' && s[i][j] <= '9' && !p[i][j]) {
      bool b[h][w];
      r(k, h) r(l, w) b[k][l] = 0;
      queue<pair<int, int>> q;
      string ss[h][w];
      q.push(mk(i, j));
      while (!q.empty()) {
        pair<int, int> a = q.front();
        q.pop();
        int x = a.second, y = a.first;
        p[y][x] = 1;
        ss[y][x] += s[y][x];
        r(k, 2) {
          int x1 = x + dx[k], y1 = y + dy[k];
          if (x1 < 0 || y1 < 0 || x1 >= w || y1 >= h || s[y1][x1] < '0' ||
              s[y1][x1] > '9')
            continue;
          if (b[y1][x1] && ss[y1][x1] <= ss[y][x]) {
            ss[y1][x1] = "";
            ss[y1][x1] += ss[y][x];
          } else {
            ss[y1][x1] += ss[y][x];
            q.push(mk(y1, x1));
            b[y1][x1] = 1;
          }
        }
      }
      string tt = "";
      r(k, h) r(l, w) tt = max(tt, ss[k][l]);
      if (t.size() < tt.size())
        if (t < tt)
          t = tt;
      // cout<<t<<endl;
    }
    cout << t << endl;
  }
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define int long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define fr first
#define sc second
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define All(v) v.begin(), v.end()

typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
const int INF = 1107110711071107;

int h, w;
char c[71][71];

signed main() {
  while (cin >> w >> h, w | h) {
    vector<string> dp[71];
    Rep(i, h) dp[i] = vector<string>(71);
    Rep(i, h) Rep(j, w) {
      cin >> c[i][j];
      dp[i][j] = "0";
    }

    Rep(i, h) Rep(j, w) {
      if ('0' <= c[i][j] && c[i][j] <= '9')
        dp[i][j] = c[i][j];
    }

    for (int i = 0; i < h; i++) {
      Rep(j, w) {
        if ('0' <= c[i][j] && c[i][j] <= '9') {
          if (i && dp[i - 1][j] != "0") {
            if (dp[i][j].size() <= dp[i - 1][j].size()) {
              dp[i][j] = dp[i - 1][j] + c[i][j];
            } else if (dp[i][j].size() - 1 == dp[i - 1][j].size()) {
              dp[i][j] = max(dp[i][j], dp[i - 1][j] + c[i][j]);
            }
          }
          if (j && dp[i][j - 1] != "0") {
            if (dp[i][j].size() <= dp[i][j - 1].size()) {
              dp[i][j] = dp[i][j - 1] + c[i][j];
            } else if (dp[i][j].size() - 1 == dp[i][j - 1].size()) {
              dp[i][j] = max(dp[i][j], dp[i][j - 1] + c[i][j]);
            }
          }
        }
      }
    }

    string ans = "";
    Rep(i, h) Rep(j, w) {
      if (ans.size() < dp[i][j].size())
        ans = dp[i][j];
      else if (ans.size() == dp[i][j].size())
        ans = max(ans, dp[i][j]);
    }

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

#define int long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define fr first
#define sc second
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define All(v) v.begin(), v.end()

typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
const int INF = 1107110711071107;

int used[71][71];
char c[71][71];
string ans;
int h, w;

void func(string now, int y, int x) {
  // cout << now << " " << y << " " << x << endl;
  if (ans.size() < now.size())
    ans = now;
  else if (ans.size() <= now.size())
    ans = max(ans, now);
  if (used[y][x] > now.size())
    return;
  used[y][x] = now.size();
  int ny = y + 1, nx = x;
  if (!(ny < 0 || nx < 0 || ny >= h || nx >= w)) {
    if ('0' <= c[ny][nx] && c[ny][nx] <= '9')
      func(now + c[ny][nx], ny, nx);
  }
  ny = y;
  nx = x + 1;
  if (!(ny < 0 || nx < 0 || ny >= h || nx >= w)) {
    if ('0' <= c[ny][nx] && c[ny][nx] <= '9')
      func(now + c[ny][nx], ny, nx);
  }
}

signed main() {
  while (cin >> w >> h, w | h) {
    memset(used, 0, sizeof(used));
    ans = "";
    Rep(i, h) Rep(j, w) cin >> c[i][j];

    Rep(i, h) Rep(j, w) {
      if (used[i][j] > 0)
        continue;
      if ('1' <= c[i][j] && c[i][j] <= '9') {
        string s = "";
        s += c[i][j];
        func(s, i, j);
      }
    }

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

#include <iostream>
#include <string>
using namespace std;
int w, h;
string str[71];
string mp[71][71];

int check(string a, string b) {
  //  cout << a <<" | "<<b<<endl;

  if (a.size() < b.size())
    return 1;
  if (a.size() > b.size())
    return 2;

  for (int i = 0; i < a.size(); i++) {
    if (a[i] - '0' < b[i] - '0')
      return 1;
    if (a[i] - '0' > b[i] - '0')
      return 2;
  }

  return 2;
}

int main() {

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++)
      cin >> str[i];

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if ('0' <= str[i][j] && str[i][j] <= '9')
          mp[i][j] = str[i][j];
        else
          mp[i][j] = "x";
      }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (mp[i][j] == "x")
          continue;
        string t = mp[i][j];
        int flg = 0;
        if (j - 1 >= 0 && '0' <= mp[i][j - 1][0] && mp[i][j - 1][0] <= '9' &&
            mp[i][j - 1] != "0") {
          mp[i][j] = mp[i][j - 1] + t;
          // cout <<mp[i][j] <<endl;
          flg = 1;
        }
        if (i - 1 >= 0 && '0' <= mp[i - 1][j][0] && mp[i - 1][j][0] <= '9' &&
            mp[i - 1][j] != "0") {
          if (flg == 1) {
            if (check(mp[i][j], mp[i - 1][j] + t) == 1)
              mp[i][j] = mp[i - 1][j] + t;
          } else
            mp[i][j] = mp[i - 1][j] + t;
          // cout << mp[i-1][j]+t<<endl;
        }
      }
    }

    string ans = "0";
    for (int i = h - 1; i >= 0; i--) {
      for (int j = w - 1; j >= 0; j--) {
        if (mp[i][j] == "x")
          continue;
        if (check(ans, mp[i][j]) == 1)
          ans = mp[i][j];
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
string ans;
string map[71];
int w, h;

int check(string a, string b) {

  if (a.size() < b.size())
    return 1;
  else if (a.size() > b.size())
    return 0;

  for (int i = 0; i < b.size(); i++) {
    if (a[i] < b[i])
      return 1;
    else if (a[i] > b[i])
      return 0;
  }

  return 0;
}

void dfs(int y, int x, string sum) {
  sum += map[y][x];

  if (y + 1 < h && '0' <= map[y + 1][x] && map[y + 1][x] <= '9')
    dfs(y + 1, x, sum);
  if (x + 1 < w && '0' <= map[y][x + 1] && map[y][x + 1] <= '9')
    dfs(y, x + 1, sum);
  else if (check(ans, sum) == 1)
    ans = sum; //,cout <<"ans="<< ans <<endl;
}

int main() {

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    for (int i = 0; i < h; i++)
      cin >> map[i];
    ans = "0";

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if ('0' < map[i][j] && map[i][j] <= '9')
          dfs(i, j, "");
      }

    cout << ans << endl;
  }

  return 0;
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

std::string str[128][128];
char field[128][128];
int W, H;

void Load() {
  scanf("%d %d", &W, &H);
  for (int i = 0; i < H; ++i) {
    scanf("%s", field[i]);
  }
}

void init() {
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      str[i][j].clear();
    }
  }
}
std::string &greater(std::string &a, std::string &b) {
  if (a.size() != b.size()) {
    if (a.size() < b.size())
      return b;
    else
      return a;
  }
  if (a < b) {
    return b;
  }
  return a;
}

void solve() {
  for (int r = 0; r < H; ++r) {
    for (int c = 0; c < W; ++c) {
      if (not('0' <= field[r][c] and field[r][c] <= '9'))
        continue;
      std::string t1;
      std::string t2;
      if (0 <= c - 1) {
        t1 = str[r][c - 1];
      }
      if (0 <= r - 1) {
        t2 = str[r - 1][c];
      }
      std::string t = greater(t1, t2);
      if (t.size() == 0 and field[r][c] == '0')
        continue;
      str[r][c] = greater(t1, t2) + field[r][c];
    }
  }
}

int main() {
  for (;;) {
    init();
    Load();
    if (W == 0 and H == 0)
      break;
    solve();
    std::string ans;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        ans = greater(ans, str[i][j]);
      }
    }
    if (ans.empty()) {
      std::cout << 0 << std::endl;
    }
    std::cout << ans << std::endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

typedef unsigned long long ull;
#define rep(i, n) REP(i, 0, n)
#define REP(i, a, n) for (int i = (a); i < (n); i++)

int w, h;
char c[70][70];
#define is_ok(i, j) (0 <= i && i < h && 0 <= j && j < w && isdigit(c[i][j]))

set<string> search(int i, int j) {
  if (!is_ok(i, j))
    return {};
  set<string> ret = {};
  for (auto s : search(i + 1, j))
    ret.insert(c[i][j] + s);
  for (auto s : search(i, j + 1))
    ret.insert(c[i][j] + s);
  if (ret.empty())
    ret.insert(string(1, c[i][j]));
  return ret;
}

string clean(string s) {
  int i;
  for (i = 0; s[i] == '0' && i < s.size(); i++)
    ;
  return s.substr(i, s.size() - i);
}

int main() {
  while (cin >> w >> h, w + h) {
    rep(i, h) scanf("%s", &c[i]);
    set<string> codes = {};
    rep(i, h) rep(j, w) if (!is_ok(i - 1, j) && !is_ok(i, j - 1) &&
                            is_ok(i, j)) for (auto s : search(i, j))
        codes.insert(clean(s));

    size_t maxlen = 0;
    for (auto s : codes)
      maxlen = max(maxlen, s.size());
    for (auto it = codes.begin(); it != codes.end();)
      if (it->size() != maxlen)
        it = codes.erase(it);
      else
        it++;
    cout << *codes.rbegin() << endl;
  }
}
Problem p00707 (C++) — buggy submission, verdict: Time Limit Exceeded

///
// File:  1126.cpp
// Author: ymiyamoto
//
// Created on Sun Dec 10 00:54:34 2017
//

#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
bool bigger(string str1, string str2) {
  if (str1.size() == str2.size()) {
    return str1 < str2;
  } else {
    return str1.size() < str2.size();
  }
}

string wfs(vector<string> matrix, vector<vector<string>> &memo, uint32_t i,
           uint32_t j) {
  if (i < matrix.size() && j < matrix[0].size() && memo[i][j] != "")
    return memo[i][j];

  if (i >= matrix.size() || j >= matrix[0].size() || !isdigit(matrix[i][j]))
    return "";
  else {
    string s1 = wfs(matrix, memo, i + 1, j);
    string s2 = wfs(matrix, memo, i, j + 1);
    return memo[i][j] = matrix[i][j] + (bigger(s1, s2) ? s2 : s1);
  }
}

int32_t main() {
  while (true) {
    uint32_t W, H;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    vector<string> matrix;
    for (uint32_t i = 0; i < H; i++) {
      string line;
      cin >> line;
      matrix.push_back(line);
    }

    string number = "";
    vector<vector<string>> memo(H, vector<string>(W, ""));
    for (int32_t i = matrix.size() - 1; i >= 0; i--) {
      for (int32_t j = matrix[0].size() - 1; j >= 0; j--) {
        if (matrix[i][j] == '0')
          continue;
        string num = wfs(matrix, memo, i, j);
        if (bigger(number, num)) {
          number = num;
        }
      }
    }
    cout << number << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

///
// File:  1126.cpp
// Author: ymiyamoto
//
// Created on Sun Dec 10 00:54:34 2017
//

#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
bool bigger(string str1, string str2) {
  if (str1.size() == str2.size()) {
    return str1 < str2;
  } else {
    return str1.size() < str2.size();
  }
}

string wfs(vector<string> matrix, vector<vector<string>> &memo, uint32_t i,
           uint32_t j, string aux) {
  if (i < matrix.size() && j < matrix[0].size() && memo[i][j] != "")
    return memo[i][j];

  if (i >= matrix.size() || j >= matrix[0].size() || !isdigit(matrix[i][j]))
    return aux;
  else {
    string number(aux);
    if (number == "" && matrix[i][j] == '0') {
    } else {
      number += matrix[i][j];
    }
    string s1 = wfs(matrix, memo, i + 1, j, number);
    string s2 = wfs(matrix, memo, i, j + 1, number);
    return memo[i][j] = bigger(s1, s2) ? s2 : s1;
  }
}

int32_t main() {
  while (true) {
    uint32_t W, H;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    vector<string> matrix;
    for (uint32_t i = 0; i < H; i++) {
      string line;
      cin >> line;
      matrix.push_back(line);
    }

    string number = "";
    vector<vector<string>> memo(H, vector<string>(W, ""));
    for (uint32_t i = 0; i < matrix.size(); i++) {
      for (uint32_t j = 0; j < matrix[0].size(); j++) {
        string num = wfs(matrix, memo, i, j, "");
        if (bigger(number, num)) {
          number = num;
        }
      }
    }
    cout << number << endl;
  }

  return 0;
}
Problem p00707 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int W, H;
char c[71][71];
string s[71][71];
string t = ".";

bool operation(const string &l, const string &r) {
  if (l.size() != r.size()) {
    return l.size() < r.size();
  } else {
    return l < r;
  }
}

string solve(int i, int j) {
  string s1, s2;
  s1 = s2 = c[i][j];
  if (s[i][j] != t)
    return s[i][j];
  if (c[i][j + 1] >= '0' && c[i][j + 1] <= '9' && j + 1 < W) {
    if (s[i][j + 1] != t)
      s1 += s[i][j + 1];
    else
      s1 += solve(i, j + 1);
  }
  if (c[i + 1][j] >= '0' && c[i + 1][j] <= '9' && i + 1 < H) {
    if (s[i + 1][j] != t)
      s2 += s[i + 1][j];
    else
      s2 += solve(i + 1, j);
  }

  s[i][j] = max(s1, s2, operation);
  return s[i][j];
}

int main() {
  string res;
  while (1) {
    cin >> W >> H;
    if (W == 0)
      break;
    res = "";
    for (int i = 0; i < 71; i++) {
      for (int j = 0; j < 71; j++) {
        s[i][j] = ".";
      }
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> c[i][j];
      }
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (c[i][j] >= '1' && c[i][j] <= '9') {
          solve(i, j);
          res = max(res, s[i][j], operation);
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int W, H;
char c[71][71];
string s[71][71];
string t = ".";

bool operation(const string &l, const string &r) {
  if (l.size() != r.size()) {
    return l.size() < r.size();
  } else {
    return l < r;
  }
}

string solve(int i, int j) {
  string s1, s2;
  s1 = s2 = c[i][j];
  if (s[i][j] != t)
    return s[i][j];
  if (c[i][j + 1] >= '0' && c[i][j + 1] <= '9' && j + 1 < W) {
    if (s[i][j + 1] != t)
      s1 += s[i][j + 1];
    else
      s1 += solve(i, j + 1);
  }
  if (c[i + 1][j] >= '0' && c[i + 1][j] <= '9' && i + 1 < H) {
    if (s[i + 1][j] != t)
      s2 += s[i + 1][j];
    else
      s2 += solve(i + 1, j);
  }

  s[i][j] = max(s1, s2, operation);
  return s[i][j];
}

int main() {
  while (1) {
    cin >> W >> H;
    int x, y;
    string st, res;
    if (W == 0)
      break;
    res = "";
    for (int i = 0; i < 71; i++) {
      for (int j = 0; j <= 71; j++) {
        s[i][j] = ".";
      }
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> c[i][j];
      }
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (c[i][j] >= '1' && c[i][j] <= '9') {
          solve(i, j);
        }
      }
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (s[i][j] == t)
          continue;
        res = max(res, s[i][j], operation);
      }
    }

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

#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;
typedef long long ll;

char field[71][71];
string dp[71][71];

bool str_less(const string &a, const string &b) {
  if (a.size() < b.size()) {
    return true;
  }
  if (a.size() > b.size()) {
    return false;
  }
  return a < b;
}

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0) {
      break;
    }
    memset(field, 0, sizeof(field));
    for (int i = 0; i < H; ++i) {
      cin >> field[i];
    }
    for (int i = 0; i <= H; ++i) {
      for (int j = 0; j <= W; ++j) {
        dp[i][j] = "";
      }
    }
    string answer = "";
    for (int i = H - 1; i >= 0; --i) {
      for (int j = W - 1; j >= 0; --j) {
        if (isdigit(field[i][j])) {
          if (str_less(dp[i + 1][j], dp[i][j + 1])) {
            dp[i][j] = field[i][j] + dp[i][j + 1];
          } else {
            dp[i][j] = field[i][j] + dp[i + 1][j];
          }
          string value = "";
          for (int k = 0; k < dp[i][j].size(); ++k) {
            if (dp[i][j][k] != '0') {
              value = string(dp[i][j].c_str() + k);
              break;
            }
          }
          if (str_less(answer, value)) {
            answer = value;
          }
        }
      }
    }
    cout << answer << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;
typedef long long ll;

char field[71][71];

string recur(string value, int x, int y) {
  if (isdigit(field[y][x])) {
    value += field[y][x];
    string src = value, next;
    next = recur(src, x + 1, y);
    if (value.size() < next.size() ||
        (value.size() == next.size() && value < next)) {
      value = next;
    }
    next = recur(src, x, y + 1);
    if (value.size() < next.size() ||
        (value.size() == next.size() && value < next)) {
      value = next;
    }
  }
  return value;
}

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;
    if (W == 0 && H == 0) {
      break;
    }
    memset(field, 0, sizeof(field));
    for (int i = 0; i < H; ++i) {
      cin >> field[i];
    }
    string answer = "";
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (field[i][j] != '0') {
          string value = recur("", j, i);
          if (answer.size() < value.size() ||
              (answer.size() == value.size() && answer < value)) {
            answer = value;
          }
        }
      }
    }
    cout << answer << endl;
  }
  return 0;
}