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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define REP(NAME, NUM) for (int NAME = 0; NAME < int(NUM); ++NAME)
#define BREP(NAME, NUM) for (int NAME = (NUM)-1; NAME >= 0; --NAME)
#define ALL(NAME) (NAME).begin(), (NAME).end()

#define cMOD 1000000007ULL
#define cINF ((1ll<<62)-1)
#define cINFINT ((1<<30)-1)

template <typename T>
void chmin(T& a, T b){ if(a > b) a = b; }
template <typename T>
void chmax(T& a, T b){ if(a < b) a = b; }

ll ss(ll i, ll n)
{
    if(i < n) return 0;
    else if(n <= i && i < 2*n) return i - (n-1);
    else if(2*n <= i && i < 3*n) return 3*n-1-i;
    else return 0;
}

int main()
{
    cin.tie( 0 ); ios::sync_with_stdio( false );
    // ----------------------------------------------------------------
    
    ll n,k;
    cin >> n>>k;

    ll s = 0;
    bool ok = false;
    REP(i,3*n-2)
    {
        s += ss(i+n, n);
        s -= ss(i, n);

        if(k > s)
        {
            k -= s;
            continue;
        }
        else
        {
            REP(j, n)
            {
                ll s2 = ss(i+n-j, n);
                if(k > s2)
                {
                    k -= s2;
                    continue;
                }
                else
                {
                    ll a = j;
                    ll c = min(i-a, n-1)-(k-1);
                    ll b = i - a - c;
                    cout << a+1 << " " << b+1 << " " << c+1 << endl;
                    ok = true;
                    break;
                }
            }
        }
        if(ok) break;
    }
    
    // ----------------------------------------------------------------
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,w;cin >> h >> w;
    vector<string> s(h);
    dsu uf(h+w);
    uf.merge(0,h);
    uf.merge(h-1,h+w-1);
    uf.merge(h,h+w-1);
    REP(i,h){
        cin >> s[i];
        REP(j,w){
            if(s[i][j]=='#') uf.merge(i,j+h);    
        }
    }
    map<int,int> mh,mw;
    REP(i,h) mh[uf.leader(i)]++;
    REP(i,w) mw[uf.leader(i+h)]++;
    cout << min(mh.size(),mw.size())-1 << endl;
}