OI XXXIII - dos

// https://sio2.mimuw.edu.pl/c/oi33-1/p/

#include <bits/stdc++.h>

// using namespace std;

// #define GARY_DBG
#define GARY_LIB

// #define int long long

constexpr int sizik = 1007;
constexpr int INF = 2 * 1001 * 1000;

#define ar std::array
#define pr std::pair

namespace SegTree {

constexpr int L = 0;
constexpr int R = INF;

class SegTree {
    struct Node {
        Node *left = nullptr, *right = nullptr;
        int cnt = 0;
        int wyn = L;
    };
    Node* root = nullptr;

    void update(Node*& t, int l, int r, int pos, int delta) {
        if (!t) t = new Node();
        if (l == r) {
            t->cnt += delta;
            if (t->cnt > 0) {
                t->wyn = l + t->cnt - 1;
            } else {
                t->wyn = L;
            }
            return;
        }
        int tm = (l + r) / 2;
        if (pos <= tm)
            update(t->left, l, tm, pos, delta);
        else
            update(t->right, tm + 1, r, pos, delta);

        int left_cnt = t->left ? t->left->cnt : 0;
        int right_cnt = t->right ? t->right->cnt : 0;
        int left_wyn = t->left ? t->left->wyn : L;
        int right_wyn = t->right ? t->right->wyn : L;
        t->cnt = left_cnt + right_cnt;

        int right_candidate = right_wyn;
        int left_candidate = (left_wyn == L) ? L : left_wyn + right_cnt;
        t->wyn = std::max(left_candidate, right_candidate);
    }

public:
    void insert(int x) { update(root, L, R, x, +1); }
    void erase_one(int x) { update(root, L, R, x, -1); }
    int get_max() const { return root ? root->wyn : L; }
};

} // namespace SegTree

constexpr int NIC = 0;
constexpr int PRZESZKODA = 1;
constexpr int WOLNE_POLE = 2;
constexpr int FORT = 3;

// [y][x]
int mapka[sizik][sizik];
int bfs_map[sizik][sizik];
ar<std::bitset<sizik>, sizik> visited;

bool is_przeszkoda(int y, int x) {
    return mapka[y][x] == PRZESZKODA || mapka[y][x] == NIC;
}

void bfs() {
    std::queue<ar<int, 3>> q;
    q.push({1, 1, 0});
    while (!q.empty()) {
        auto [y, x, d] = q.front();
        q.pop();

        if (visited[y][x]) continue;
        visited[y][x] = true;

        bfs_map[y][x] = d;

        if (!is_przeszkoda(y - 1, x)) {
            q.push({y - 1, x, d + 1});
        }
        if (!is_przeszkoda(y + 1, x)) {
            q.push({y + 1, x, d + 1});
        }
        if (!is_przeszkoda(y, x - 1)) {
            q.push({y, x - 1, d + 1});
        }
        if (!is_przeszkoda(y, x + 1)) {
            q.push({y, x + 1, d + 1});
        }
    }
}

SegTree::SegTree ds;
int elements_added = 0;

void update(int y, int x) {
    if (mapka[y][x] == FORT) {
        ds.erase_one(bfs_map[y][x]);
        elements_added--;
        mapka[y][x] = WOLNE_POLE;
    } else if (mapka[y][x] == WOLNE_POLE) {
        ds.insert(bfs_map[y][x]);
        elements_added++;
        mapka[y][x] = FORT;
    } else {
        std::cout << "NOT GOOD\n";
    }
}

void ans_q() {
    // return ds.query();
    if (elements_added == 0) {
        std::cout << "0\n";
        return;
    }
    int64_t ans = ds.get_max();
    std::cout << ans << '\n';
}

void solve() {
    int n, q;
    std::cin >> n >> q;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            char c;
            std::cin >> c;

            if (c == '#') {
                mapka[i][j] = PRZESZKODA;
            } else if (c == 'Z' || c == '.') {
                mapka[i][j] = WOLNE_POLE;
            } else {
                assert(c == 'F');
                mapka[i][j] = FORT;
            }
        }
    }

    bfs();

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (mapka[i][j] == FORT) {
                ds.insert(bfs_map[i][j]);
                elements_added++;
            }
        }
    }

    ans_q();

    for (; q > 0; q--) {
        int x, y;
        std::cin >> y >> x;

        update(y, x);

        ans_q();
    }
}

int32_t main() {
#ifndef GARY_DBG
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
#endif

    int t = 1;
    // std::cin >> t;

    for (; t > 0; t--) {
        solve();
    }

    return 0;
}