Problem p00481 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
// #include <functional>
#include <cstdio>
#include <fstream>
#include <queue>
using namespace std;

#define START 'S'
#define CANTGO 'X'
#define NONE '.'
#define FINUM field[i][j] - '0'

struct Point {
  int x;
  int y;
};

int dx[] = {0, -1, 0, 1}; // ã©çvñè
int dy[] = {1, 0, -1, 0};
string field[1000];
int mfield[1001][1001] = {};
int life = 1;
int csum = 0;
int H, W, N;

int open(Point gool) {
  int counter = 0;
  queue<Point> tp;
  tp.push(gool);
  int i, j;
  Point inp;
  Point p;
  while (1) {
    counter++;
    int size = tp.size();
    for (i = 0; i < size; i++) {
      p = tp.front();
      tp.pop();
      for (j = 0; j < 4; j++) {
        inp.x = p.x + dx[j];
        inp.y = p.y + dy[j];
        if (inp.x < 0 || inp.y < 0 || inp.y >= H || inp.x >= W)
          continue;
        if (field[inp.y][inp.x] != CANTGO && mfield[inp.y][inp.x] != life) {
          // v·éÉÅZ£Ú®Â\][
          if (field[inp.y][inp.x] ==
              life + '0') // ©ªÉ©Á½`[YÌ­©
          {
            return counter;
          } else {
            tp.push(inp);
            mfield[inp.y][inp.x] = life;
          }
        }
      }
    }
    /*for(i=0;i<H;i++)
    {
            for(j=0;j<W;j++)
            {
                    cout << mfield[i][j];
            }
            cout << endl;
    }*/
  }
  // copy(tp.begin() , tp.end() , tp.begin());
  return 0;
}

int main(void) {
  int i, j;
  queue<Point> tp;
  Point top[10];
  Point inp;
  int count = 0;
  int counter = 0;
  cin >> H >> W >> N;

  for (i = 0; i < H; i++) // üÍ
  {
    cin >> field[i];
    for (j = 0; j < W; j++) {
      if (field[i][j] == 'S') {
        top[0].y = i;
        top[0].x = j;
        mfield[j][i] = life;
      }
      if (field[i][j] >= '0' && field[i][j] <= '9') {
        top[field[i][j] - '0'].y = i;
        top[field[i][j] - '0'].x = j;
      }
    }
  } // üÍIíè

  while (1) {
    if ((counter = open(top[life - 1])) != 0) {
      life++;
      csum += counter;
    }
    if (life > N)
      break;
  }
  cout << csum << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
// #include <functional>
#include <cstdio>
#include <fstream>
#include <queue>
using namespace std;

#define START 'S'
#define CANTGO 'X'
#define NONE '.'
#define FINUM field[i][j] - '0'

struct Point {
  int x;
  int y;
};

int dx[] = {0, -1, 0, 1}; // ã©çvñè
int dy[] = {1, 0, -1, 0};
string field[1000];
int mfield[1001][1001] = {};
int life = 1;
int csum = 0;
int H, W, N;

int open(Point gool) {
  int counter = 0;
  queue<Point> tp;
  tp.push(gool);
  int i, j;
  Point inp;
  Point p;
  while (1) {
    counter++;
    int size = tp.size();
    for (i = 0; i < size; i++) {
      p = tp.front();
      tp.pop();
      for (j = 0; j < 4; j++) {
        inp.x = p.x + dx[j];
        inp.y = p.y + dy[j];
        if (inp.x < 0 || inp.y < 0 || inp.y >= H || inp.x >= W)
          continue;
        if (field[inp.y][inp.x] != CANTGO && mfield[inp.y][inp.x] != life) {
          // v·éÉÅZ£Ú®Â\][
          if (field[inp.y][inp.x] ==
              life + '0') // ©ªÉ©Á½`[YÌ­©
          {
            return counter;
          } else {
            tp.push(inp);
            mfield[inp.y][inp.x] = life;
          }
        }
      }
    }
    /*for(i=0;i<H;i++)
    {
            for(j=0;j<W;j++)
            {
                    cout << mfield[i][j];
            }
            cout << endl;
    }*/
  }
  // copy(tp.begin() , tp.end() , tp.begin());
  return 0;
}

int main(void) {
  int i, j;
  queue<Point> tp;
  Point top[10];
  Point inp;
  int count = 0;
  int counter = 0;
  cin >> H >> W >> N;

  for (i = 0; i < H; i++) // üÍ
  {
    cin >> field[i];
    for (j = 0; j < W; j++) {
      if (field[i][j] == 'S') {
        top[0].y = i;
        top[0].x = j;
        mfield[i][j] = life;
      }
      if (field[i][j] >= '0' && field[i][j] <= '9') {
        top[field[i][j] - '0'].y = i;
        top[field[i][j] - '0'].x = j;
      }
    }
  } // üÍIíè

  while (1) {
    if ((counter = open(top[life - 1])) != 0) {
      life++;
      csum += counter;
    }
    if (life > N)
      break;
  }
  cout << csum << endl;

  return 0;
}