15_D. Map

There is an area map that is a rectangular matrix n × m, each cell of the matrix contains the average height of a corresponding area part. Peter works for a company that has to build several cities within this area, each of the cities will occupy a rectangle a × b cells on the map. To start construction works in a particular place Peter needs to remove excess ground from the construction site where a new city will be built. To do so he chooses a cell of the minimum height within this site, and removes excess ground from other cells of the site down to this minimum level. Let's consider that to lower the ground level from h2 to h1 (h1 ≤ h2) they need to remove h2 - h1 ground units.

Let's call a site's position optimal, if the amount of the ground removed from this site is minimal compared to other possible positions. Peter constructs cities according to the following algorithm: from all the optimum site's positions he chooses the uppermost one. If this position is not unique, he chooses the leftmost one. Then he builds a city on this site. Peter repeats this process untill he can build at least one more city. For sure, he cannot carry out construction works on the occupied cells. Would you, please, help Peter place cities according to the algorithm? 

Input

The first line contains four space-separated integers: map sizes n, m and city sizes a, b (1 ≤ a ≤ n ≤ 1000, 1 ≤ b ≤ m ≤ 1000). Then there follow n lines, each contains m non-negative space-separated numbers, describing the height matrix. Each number doesn't exceed 109. 

Output

In the first line output k — the amount of constructed cities. In each of the following k lines output 3 space-separated numbers — the row number and the column number of the upper-left corner of a subsequent construction site, and the amount of the ground to remove from it. Output the sites in the order of their building up.

Examples

Input

2 2 1 2
1 2
3 5


Output

2
1 1 1
2 1 2


Input

4 4 2 2
1 5 3 4
2 7 6 1
1 1 2 2
2 2 1 2


Output

3
3 1 2
3 3 3
1 2 9

Example input:
4 4 2 2
1 5 3 4
2 7 6 1
1 1 2 2
2 2 1 2

2 2 1 2
1 2
3 5


Example output:
3
3 1                                                                2
3 3                                                                3
1 2                                                                9

2
1 1                                                                1
2 1                                                                2


Tags: data structures
implementation
sortings
Difficulty rating: 2500

Solution:
#include <bits/stdc++.h>
using namespace std;
int a[1010][1010], b[1010][1010], n, m, r, c;
long long s[1010][1010];
bool vis[1010][1010];
struct node {
  int x, y;
  long long v;
  bool operator<(const node &p) const {
    if (v != p.v) return v < p.v;
    if (x != p.x) return x < p.x;
    return y < p.y;
  }
} p[1010 * 1010];
int main() {
  scanf("%d%d%d%d", &n, &m, &r, &c);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", &a[i][j]), s[i][j] += s[i][j - 1] + a[i][j];
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) s[j][i] += s[j - 1][i];
  for (int i = 1; i <= n; i++) {
    multiset<int> st;
    for (int j = 1; j <= m; j++) {
      if (j > c) st.erase(st.find(a[i][j - c]));
      st.insert(a[i][j]);
      b[i][j] = *st.begin();
    }
  }
  for (int i = 1; i <= m; i++) {
    multiset<int> st;
    for (int j = 1; j <= n; j++) {
      if (j > r) st.erase(st.find(b[j - r][i]));
      st.insert(b[j][i]);
      a[j][i] = *st.begin();
    }
  }
  int cnt = 0;
  for (int i = r; i <= n; i++)
    for (int j = c; j <= m; j++) {
      long long tmp = s[i][j] - s[i - r][j] - s[i][j - c] + s[i - r][j - c];
      p[cnt++] = (node){i, j, tmp - (long long)a[i][j] * (long long)r * c};
    }
  sort(p, p + cnt);
  int num = 0;
  for (int i = 0; i < cnt; i++)
    if (!vis[p[i].x][p[i].y]) {
      for (int x = min(p[i].x + r - 1, n); x > p[i].x - r; x--)
        for (int y = min(p[i].y + c - 1, m); y > p[i].y - c; y--) vis[x][y] = 1;
      p[num++] = p[i];
    }
  cout << num << endl;
  for (int i = 0; i < num; i++)
    printf("%d %d %I64d\n", p[i].x - r + 1, p[i].y - c + 1, p[i].v);
  return 0;
}