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

#define ll long long;

int main(){
  int h,w,x,y;
  cin>>h>>w>>x>>y;
  char a[h][w];
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin>>a[i][j];
    }
  }
  x--;y--;
  int ans=0;
  if(a[x][y]=='#'){
      ans=0;
  }
  else{
    ans++;
    for(int i=x-1;i>=0;i--){
      if(a[i][y]=='#'){
        break;
      }
      ans++;
    }
    for(int i=x+1;i<h;i++){
      if(a[i][y]=='#'){
        break;
      }
      ans++;
    }
    for(int i=y-1;i>=0;i--){
      if(a[x][i]=='#'){
        break;
      }
      ans++;
    }
    for(int i=y+1;i<w;i++){
      if(a[x][i]=='#'){
        break;
      }
      ans++;
    }
    cout<<ans<<endl;
  }


  return 0;
}
#include<bits/stdc++.h>
using namespace std;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pint;
typedef pair<ll,ll> plint;
const int mod = 1000000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

const int N = 2e5 + 10; 

int n, m;
vector<pint>g[N];
map<pint,int>st; 
int col[N];

void dfs(int u, int fa){
	for (int i = 0; i < g[u].size(); ++i) {
		int v = g[u][i].fi, w = g[u][i].se;
		if (col[v]) continue;
		if (w == col[u]) {
			col[v] = ((col[u] == n) ? 1 : col[u] + 1);
		} else {
			col[v] = w;
		}
		dfs(v, u);
	}
}

int main()
{
	cin >> n >> m;
	rep(i,0,m) {
		int u,v,w;
		cin >> u >> v >> w;
		if (st[{u,v}]) continue;
		g[u].pb({v,w});
		g[v].pb({u,w});
		st[{u,v}] = st[{v,u}] = 1;
	}
	col[1] = 1;
	dfs(1,0);
	rep (i,1,n + 1) cout << col[i] << '\n';
    return 0;
}